Index: uspace/srv/fs/fat/fat_ops.c
===================================================================
--- uspace/srv/fs/fat/fat_ops.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/fs/fat/fat_ops.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -1527,6 +1527,25 @@
 void fat_sync(ipc_callid_t rid, ipc_call_t *request)
 {
-	/* Dummy implementation */
-	ipc_answer_0(rid, EOK);
+	dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
+	fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
+	
+	fs_node_t *fn;
+	int rc = fat_node_get(&fn, dev_handle, index);
+	if (rc != EOK) {
+		ipc_answer_0(rid, rc);
+		return;
+	}
+	if (!fn) {
+		ipc_answer_0(rid, ENOENT);
+		return;
+	}
+	
+	fat_node_t *nodep = FAT_NODE(fn);
+	
+	nodep->dirty = true;
+	rc = fat_node_sync(nodep);
+	
+	fat_node_put(fn);
+	ipc_answer_0(rid, rc);
 }
 
Index: uspace/srv/fs/tmpfs/tmpfs_ops.c
===================================================================
--- uspace/srv/fs/tmpfs/tmpfs_ops.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/fs/tmpfs/tmpfs_ops.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -736,5 +736,8 @@
 void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
 {
-	/* Dummy implementation */
+	/*
+	 * TMPFS keeps its data structures always consistent,
+	 * thus the sync operation is a no-op.
+	 */
 	ipc_answer_0(rid, EOK);
 }
Index: uspace/srv/hid/console/gcons.c
===================================================================
--- uspace/srv/hid/console/gcons.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hid/console/gcons.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -286,4 +286,7 @@
 	ssize_t nx = (ssize_t) mouse_x + dx;
 	ssize_t ny = (ssize_t) mouse_y + dy;
+
+	if (!use_gcons)
+		return;
 	
 	mouse_x = (size_t) limit(nx, 0, xres);
Index: uspace/srv/hid/fb/fb.c
===================================================================
--- uspace/srv/hid/fb/fb.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hid/fb/fb.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -1347,6 +1347,6 @@
 {
 	mouse_hide();
-	pointer_x = x;
-	pointer_y = y;
+	pointer_x = x % screen.xres;
+	pointer_y = y % screen.yres;
 	mouse_show();
 }
Index: uspace/srv/hid/kbd/Makefile
===================================================================
--- uspace/srv/hid/kbd/Makefile	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hid/kbd/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -60,6 +60,6 @@
 	ifeq ($(MACHINE),gta02)
 		SOURCES += \
-			port/dummy.c \
-			ctl/pc.c
+			port/chardev.c \
+			ctl/stty.c
 	endif
 	ifeq ($(MACHINE),testarm)
Index: uspace/srv/hid/kbd/port/chardev.c
===================================================================
--- uspace/srv/hid/kbd/port/chardev.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hid/kbd/port/chardev.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,4 +41,5 @@
 #include <kbd.h>
 #include <vfs/vfs.h>
+#include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -50,21 +51,41 @@
 #define NAME "kbd"
 
+/** List of devices to try connecting to. */
+static const char *in_devs[] = {
+	"/dev/char/ps2a",
+	"/dev/char/s3c24ser"
+};
+
+static const int num_devs = sizeof(in_devs) / sizeof(in_devs[0]);
+
 int kbd_port_init(void)
 {
-	const char *input = "/dev/char/ps2a";
 	int input_fd;
+	int i;
 
-	printf(NAME ": open %s\n", input);
+	input_fd = -1;
+	for (i = 0; i < num_devs; i++) {
+		struct stat s;
 
-	input_fd = open(input, O_RDONLY);
+		if (stat(in_devs[i], &s) == EOK)
+			break;
+	}
+
+	if (i >= num_devs) {
+		printf(NAME ": Could not find any suitable input device.\n");
+		return -1;
+	}
+
+	input_fd = open(in_devs[i], O_RDONLY);
 	if (input_fd < 0) {
-		printf(NAME ": Failed opening %s (%d)\n", input, input_fd);
-		return false;
+		printf(NAME ": failed opening device %s (%d).\n", in_devs[i],
+		    input_fd);
+		return -1;
 	}
 
 	dev_phone = fd_phone(input_fd);
 	if (dev_phone < 0) {
-		printf(NAME ": Failed to connect to device\n");
-		return false;
+		printf(NAME ": Failed connecting to device\n");
+		return -1;
 	}
 
@@ -73,5 +94,5 @@
 	if (ipc_connect_to_me(dev_phone, 0, 0, 0, &phonehash) != 0) {
 		printf(NAME ": Failed to create callback from device\n");
-		return false;
+		return -1;
 	}
 
Index: uspace/srv/hid/kbd/port/i8042.h
===================================================================
--- uspace/srv/hid/kbd/port/i8042.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ 	(revision )
@@ -1,55 +1,0 @@
-/*
- * Copyright (c) 2006 Josef Cejka
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** @addtogroup kbd_port
- * @ingroup  kbd
- * @{
- */
-
-/** @file
- * @brief i8042 port driver.
- */
-
-#ifndef KBD_PORT_i8042_H_
-#define KBD_PORT_i8042_H_
-
-#include <libarch/ddi.h>
-#include <libarch/types.h>
-
-struct i8042 {
-	ioport8_t data;
-	uint8_t pad[3];
-	ioport8_t status;
-} __attribute__ ((packed));
-typedef struct i8042 i8042_t;
-
-#endif
-
-/**
- * @}
- */ 
Index: uspace/srv/hid/s3c24xx_ts/Makefile
===================================================================
--- uspace/srv/hid/s3c24xx_ts/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hid/s3c24xx_ts/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,37 @@
+#
+# Copyright (c) 2010 Jiri Svoboda
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+USPACE_PREFIX = ../../..
+
+# Need to use short name because of FAT 8+3 limit
+BINARY = s3c24ts
+
+SOURCES = \
+	s3c24xx_ts.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c
===================================================================
--- uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,408 @@
+/*
+ * Copyright (c) 2010 Jiri Svoboda
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup mouse
+ * @{
+ */
+/**
+ * @file
+ * @brief Samsung Samsung S3C24xx on-chip ADC and touch-screen interface driver.
+ *
+ * This interface is present on the Samsung S3C24xx CPU (on the gta02 platform).
+ */
+
+#include <ddi.h>
+#include <libarch/ddi.h>
+#include <devmap.h>
+#include <io/console.h>
+#include <vfs/vfs.h>
+#include <ipc/ipc.h>
+#include <ipc/mouse.h>
+#include <async.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sysinfo.h>
+#include <errno.h>
+
+#include "s3c24xx_ts.h"
+
+#define NAME "s3c24ser"
+#define NAMESPACE "hid_in"
+
+static irq_cmd_t ts_irq_cmds[] = {
+	{
+		.cmd = CMD_ACCEPT
+	}
+};
+
+static irq_code_t ts_irq_code = {
+	sizeof(ts_irq_cmds) / sizeof(irq_cmd_t),
+	ts_irq_cmds
+};
+
+/** S3C24xx touchscreen instance structure */
+static s3c24xx_ts_t *ts;
+
+static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall);
+static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call);
+static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts);
+static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts);
+static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts);
+static int s3c24xx_ts_init(s3c24xx_ts_t *ts);
+static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn);
+static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y);
+static int lin_map_range(int v, int i0, int i1, int o0, int o1);
+
+int main(int argc, char *argv[])
+{
+	int rc;
+
+	printf(NAME ": S3C24xx touchscreen driver\n");
+
+	rc = devmap_driver_register(NAME, s3c24xx_ts_connection);
+	if (rc < 0) {
+		printf(NAME ": Unable to register driver.\n");
+		return -1;
+	}
+
+	ts = malloc(sizeof(s3c24xx_ts_t));
+	if (ts == NULL)
+		return -1;
+
+	if (s3c24xx_ts_init(ts) != EOK)
+		return -1;
+
+	rc = devmap_device_register(NAMESPACE "/mouse", &ts->dev_handle);
+	if (rc != EOK) {
+		devmap_hangup_phone(DEVMAP_DRIVER);
+		printf(NAME ": Unable to register device %s.\n",
+		    NAMESPACE "/mouse");
+		return -1;
+	}
+
+	printf(NAME ": Registered device %s.\n", NAMESPACE "/mouse");
+
+	printf(NAME ": Accepting connections\n");
+	task_retval(0);
+	async_manager();
+
+	/* Not reached */
+	return 0;
+}
+
+/** Initialize S3C24xx touchscreen interface. */
+static int s3c24xx_ts_init(s3c24xx_ts_t *ts)
+{
+	void *vaddr;
+	sysarg_t inr;
+
+	inr = S3C24XX_TS_INR;
+	ts->paddr = S3C24XX_TS_ADDR;
+
+	if (pio_enable((void *) ts->paddr, sizeof(s3c24xx_adc_io_t),
+	    &vaddr) != 0)
+		return -1;
+
+	ts->io = vaddr;
+	ts->client_phone = -1;
+	ts->state = ts_wait_pendown;
+	ts->last_x = 0;
+	ts->last_y = 0;
+
+	printf(NAME ": device at physical address 0x%x, inr %d.\n",
+	    ts->paddr, inr);
+
+	async_set_interrupt_received(s3c24xx_ts_irq_handler);
+	ipc_register_irq(inr, device_assign_devno(), 0, &ts_irq_code);
+
+	s3c24xx_ts_wait_for_int_mode(ts, updn_down);
+
+	return EOK;
+}
+
+/** Switch interface to wait for interrupt mode.
+ *
+ * In this mode we receive an interrupt when pen goes up/down, depending
+ * on @a updn.
+ *
+ * @param ts	Touchscreen instance
+ * @param updn	@c updn_up to wait for pen up, @c updn_down to wait for pen
+ *		down.
+ */
+static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn)
+{
+	uint32_t con, tsc;
+
+	/*
+	 * Configure ADCCON register
+	 */
+
+	con = pio_read_32(&ts->io->con);
+
+	/* Disable standby, disable start-by-read, clear manual start bit */
+	con = con & ~(ADCCON_STDBM | ADCCON_READ_START | ADCCON_ENABLE_START);
+
+	/* Set prescaler value 0xff, XP for input. */
+	con = con | (ADCCON_PRSCVL(0xff) << 6) | ADCCON_SEL_MUX(SMUX_XP);
+
+	/* Enable prescaler. */
+	con = con | ADCCON_PRSCEN;
+
+ 	pio_write_32(&ts->io->con, con);
+
+	/*
+	 * Configure ADCTSC register
+	 */
+
+	tsc = pio_read_32(&ts->io->tsc);
+
+	/* Select whether waiting for pen up or pen down. */
+	if (updn == updn_up)
+		tsc |= ADCTSC_DSUD_UP;
+	else
+		tsc &= ~ADCTSC_DSUD_UP;
+
+	/*
+	 * Enable XP pull-up and disable all drivers except YM. This is
+	 * according to the manual. This gives us L on XP input when touching
+	 * and (pulled up to) H when not touching.
+	 */
+	tsc = tsc & ~(ADCTSC_XM_ENABLE | ADCTSC_AUTO_PST |
+	    ADCTSC_PULLUP_DISABLE);
+	tsc = tsc | ADCTSC_YP_DISABLE | ADCTSC_XP_DISABLE | ADCTSC_YM_ENABLE;
+
+	/* Select wait-for-interrupt mode. */
+	tsc = (tsc & ~ADCTSC_XY_PST_MASK) | ADCTSC_XY_PST_WAITINT;
+
+	pio_write_32(&ts->io->tsc, tsc);
+}
+
+/** Handle touchscreen interrupt */
+static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call)
+{
+	ts_updn_t updn;
+
+	(void) iid; (void) call;
+
+	/* Read up/down interrupt flags. */
+	updn = pio_read_32(&ts->io->updn);
+
+	if (updn & (ADCUPDN_TSC_DN | ADCUPDN_TSC_UP)) {
+		/* Clear up/down interrupt flags. */
+		pio_write_32(&ts->io->updn, updn &
+		    ~(ADCUPDN_TSC_DN | ADCUPDN_TSC_UP));
+	}
+
+	if (updn & ADCUPDN_TSC_DN) {
+		/* Pen-down interrupt */
+		s3c24xx_ts_pen_down(ts);
+	} else if (updn & ADCUPDN_TSC_UP) {
+		/* Pen-up interrupt */
+		s3c24xx_ts_pen_up(ts);
+	} else {
+		/* Presumably end-of-conversion interrupt */
+
+		/* Check end-of-conversion flag. */
+		if ((pio_read_32(&ts->io->con) & ADCCON_ECFLG) == 0) {
+			printf(NAME ": Unrecognized ts int.\n");
+			return;
+		}
+
+		if (ts->state != ts_sample_pos) {
+			/*
+			 * We got an extra interrupt ater switching to
+			 * wait for interrupt mode.
+			 */
+			return;
+		}
+
+		/* End-of-conversion interrupt */
+		s3c24xx_ts_eoc(ts);
+	}
+}
+
+/** Handle pen-down interrupt.
+ *
+ * @param ts	Touchscreen instance
+ */
+static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts)
+{
+	/* Pen-down interrupt */
+
+	ts->state = ts_sample_pos;
+
+	/* Enable auto xy-conversion mode */
+	pio_write_32(&ts->io->tsc, (pio_read_32(&ts->io->tsc)
+	    & ~3) | 4);
+
+	/* Start the conversion. */
+	pio_write_32(&ts->io->con, pio_read_32(&ts->io->con)
+	    | ADCCON_ENABLE_START);
+}
+
+/** Handle pen-up interrupt.
+ *
+ * @param ts	Touchscreen instance
+ */
+static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts)
+{
+	int button, press;
+
+	/* Pen-up interrupt */
+
+	ts->state = ts_wait_pendown;
+
+	button = 1;
+	press = 0;
+	async_msg_2(ts->client_phone, MEVENT_BUTTON, button, press);
+
+	s3c24xx_ts_wait_for_int_mode(ts, updn_down);
+}
+
+/** Handle end-of-conversion interrupt.
+ *
+ * @param ts	Touchscreen instance
+ */
+static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts)
+{
+	uint32_t data;
+	int button, press;
+	int smp0, smp1;
+	int x_pos, y_pos;
+	int dx, dy;
+
+	ts->state = ts_wait_penup;
+
+	/* Read in sampled data. */
+
+	data = pio_read_32(&ts->io->dat0);
+	smp0 = data & 0x3ff;
+
+	data = pio_read_32(&ts->io->dat1);
+	smp1 = data & 0x3ff;
+
+	/* Convert to screen coordinates. */
+	s3c24xx_ts_convert_samples(smp0, smp1, &x_pos, &y_pos);
+
+	printf("s0: 0x%03x, s1:0x%03x -> x:%d,y:%d\n", smp0, smp1,
+	    x_pos, y_pos);
+
+	/* Get differences. */
+	dx = x_pos - ts->last_x;
+	dy = y_pos - ts->last_y;
+
+	button = 1;
+	press = 1;
+
+	/* Send notifications to client. */
+	async_msg_2(ts->client_phone, MEVENT_MOVE, dx, dy);
+	async_msg_2(ts->client_phone, MEVENT_BUTTON, button, press);
+
+	ts->last_x = x_pos;
+	ts->last_y = y_pos;
+
+	s3c24xx_ts_wait_for_int_mode(ts, updn_up);
+}
+
+/** Convert sampled data to screen coordinates. */
+static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y)
+{
+	/*
+	 * The orientation and display dimensions are GTA02-specific and the
+	 * calibration values might even specific to the individual piece
+	 * of hardware.
+	 *
+	 * The calibration values can be obtained by touching corners
+	 * of the screen with the stylus and noting the sampled values.
+	 */
+	*x = lin_map_range(smp1, 0xa1, 0x396, 0, 479);
+	*y = lin_map_range(smp0, 0x69, 0x38a, 639, 0);
+}
+
+/** Map integer from one range to another range in a linear fashion.
+ *
+ * i0 < i1 is required. i0 is mapped to o0, i1 to o1. If o1 < o0, then the
+ * mapping will be descending. If v is outside of [i0, i1], it is clamped.
+ *
+ * @param v	Value to map.
+ * @param i0	Lower bound of input range.
+ * @param i1	Upper bound of input range.
+ * @param o0	First bound of output range.
+ * @param o1	Second bound of output range.
+ *
+ * @return	Mapped value ov, o0 <= ov <= o1.
+ */
+static int lin_map_range(int v, int i0, int i1, int o0, int o1)
+{
+	if (v < i0)
+		v = i0;
+
+	if (v > i1)
+		v = i1;
+
+	return o0 + (o1 - o0) * (v - i0) / (i1 - i0);
+}
+
+/** Handle mouse client connection. */
+static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall)
+{
+	ipc_callid_t callid;
+	ipc_call_t call;
+	int retval;
+
+	ipc_answer_0(iid, EOK);
+
+	while (1) {
+		callid = async_get_call(&call);
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			if (ts->client_phone != -1) {
+				ipc_hangup(ts->client_phone);
+				ts->client_phone = -1;
+			}
+
+			ipc_answer_0(callid, EOK);
+			return;
+		case IPC_M_CONNECT_TO_ME:
+			if (ts->client_phone != -1) {
+				retval = ELIMIT;
+				break;
+			}
+			ts->client_phone = IPC_GET_ARG5(call);
+			retval = 0;
+			break;
+		default:
+			retval = EINVAL;
+		}
+		ipc_answer_0(callid, retval);
+	}
+}
+
+/** @}
+ */
Index: uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.h
===================================================================
--- uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2010 Jiri Svoboda
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup genarch
+ * @{
+ */
+/**
+ * @file
+ * @brief Samsung S3C24xx on-chip ADC and touch-screen interface driver.
+ */
+
+#ifndef S3C24XX_TS_H_
+#define S3C24XX_TS_H_
+
+#include <sys/types.h>
+
+/** S3C24xx ADC and touch-screen I/O */
+typedef struct {
+	uint32_t con;
+	uint32_t tsc;
+	uint32_t dly;
+	uint32_t dat0;
+	uint32_t dat1;
+	uint32_t updn;
+} s3c24xx_adc_io_t;
+
+/* Fields in ADCCON register */
+#define ADCCON_ECFLG		0x8000
+#define ADCCON_PRSCEN		0x4000
+
+#define ADCCON_PRSCVL(val)	(((val) & 0xff) << 6)
+
+#define ADCCON_SEL_MUX(smux)	(((smux) & 7) << 3)
+
+#define ADCCON_STDBM		0x0004
+#define ADCCON_READ_START	0x0002
+#define ADCCON_ENABLE_START	0x0001
+
+/* Values for ADCCON_SEL_MUX */
+#define SMUX_AIN0		0
+#define SMUX_AIN1		1
+#define SMUX_AIN2		2
+#define SMUX_AIN3		3
+#define SMUX_YM			4
+#define SMUX_YP			5
+#define SMUX_XM			6
+#define SMUX_XP			7
+
+
+/* Fields in ADCTSC register */
+#define ADCTSC_DSUD_UP		0x0100
+#define ADCTSC_YM_ENABLE	0x0080
+#define ADCTSC_YP_DISABLE	0x0040
+#define ADCTSC_XM_ENABLE	0x0020
+#define ADCTSC_XP_DISABLE	0x0010
+#define ADCTSC_PULLUP_DISABLE	0x0008
+#define ADCTSC_AUTO_PST		0x0004
+
+#define ADCTSC_XY_PST_NOOP	0x0000
+#define ADCTSC_XY_PST_X		0x0001
+#define ADCTSC_XY_PST_Y		0x0002
+#define ADCTSC_XY_PST_WAITINT	0x0003
+#define ADCTSC_XY_PST_MASK	0x0003
+
+/* Fields in ADCDAT0, ADCDAT1 registers */
+#define ADCDAT_UPDOWN		0x8000
+#define ADCDAT_AUTO_PST		0x4000
+
+/* Fields in ADCUPDN register */
+#define ADCUPDN_TSC_UP		0x0002
+#define ADCUPDN_TSC_DN		0x0001
+
+/** Touchscreen interrupt number */
+#define S3C24XX_TS_INR		31
+
+/** Touchscreen I/O address */
+#define S3C24XX_TS_ADDR		0x58000000
+
+typedef enum {
+	ts_wait_pendown,
+	ts_sample_pos,
+	ts_wait_penup
+} ts_state_t;
+
+typedef enum {
+	updn_up,
+	updn_down
+} ts_updn_t;
+
+/** S3C24xx touchscreen driver instance */
+typedef struct {
+	/** Physical device address */
+	uintptr_t paddr;
+
+	/** Device I/O structure */
+	s3c24xx_adc_io_t *io;
+
+	/** Callback phone to the client */
+	int client_phone;
+
+	/** Device handle */
+	dev_handle_t dev_handle;
+
+	/** Device/driver state */
+	ts_state_t state;
+
+	/** Previous position reported to client. */
+	int last_x;
+	int last_y;
+} s3c24xx_ts_t;
+
+#endif
+
+/** @}
+ */
Index: uspace/srv/hw/char/s3c24xx_uart/Makefile
===================================================================
--- uspace/srv/hw/char/s3c24xx_uart/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hw/char/s3c24xx_uart/Makefile	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,38 @@
+#
+# Copyright (c) 2005 Martin Decky
+# Copyright (c) 2007 Jakub Jermar
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+USPACE_PREFIX = ../../../..
+
+# Need to use short name because of FAT 8+3 limit
+BINARY = s3c24ser
+
+SOURCES = \
+	s3c24xx_uart.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.c
===================================================================
--- uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,216 @@
+/*
+ * Copyright (c) 2010 Jiri Svoboda
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup driver_serial
+ * @{
+ */
+/**
+ * @file
+ * @brief Samsung S3C24xx on-chip UART driver.
+ *
+ * This UART is present on the Samsung S3C24xx CPU (on the gta02 platform).
+ */
+
+#include <ddi.h>
+#include <libarch/ddi.h>
+#include <devmap.h>
+#include <ipc/ipc.h>
+#include <ipc/char.h>
+#include <async.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sysinfo.h>
+#include <errno.h>
+
+#include "s3c24xx_uart.h"
+
+#define NAME "s3c24ser"
+#define NAMESPACE "char"
+
+static irq_cmd_t uart_irq_cmds[] = {
+	{
+		.cmd = CMD_ACCEPT
+	}
+};
+
+static irq_code_t uart_irq_code = {
+	sizeof(uart_irq_cmds) / sizeof(irq_cmd_t),
+	uart_irq_cmds
+};
+
+/** S3C24xx UART instance structure */
+static s3c24xx_uart_t *uart;
+
+static void s3c24xx_uart_connection(ipc_callid_t iid, ipc_call_t *icall);
+static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call);
+static int s3c24xx_uart_init(s3c24xx_uart_t *uart);
+static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte);
+
+int main(int argc, char *argv[])
+{
+	int rc;
+
+	printf(NAME ": S3C24xx on-chip UART driver\n");
+
+	rc = devmap_driver_register(NAME, s3c24xx_uart_connection);
+	if (rc < 0) {
+		printf(NAME ": Unable to register driver.\n");
+		return -1;
+	}
+
+	uart = malloc(sizeof(s3c24xx_uart_t));
+	if (uart == NULL)
+		return -1;
+
+	if (s3c24xx_uart_init(uart) != EOK)
+		return -1;
+
+	rc = devmap_device_register(NAMESPACE "/" NAME, &uart->dev_handle);
+	if (rc != EOK) {
+		devmap_hangup_phone(DEVMAP_DRIVER);
+		printf(NAME ": Unable to register device %s.\n");
+		return -1;
+	}
+
+	printf(NAME ": Registered device %s.\n", NAMESPACE "/" NAME);
+
+	printf(NAME ": Accepting connections\n");
+	task_retval(0);
+	async_manager();
+
+	/* Not reached */
+	return 0;
+}
+
+/** Character device connection handler. */
+static void s3c24xx_uart_connection(ipc_callid_t iid, ipc_call_t *icall)
+{
+	ipc_callid_t callid;
+	ipc_call_t call;
+	ipcarg_t method;
+	int retval;
+
+	/* Answer the IPC_M_CONNECT_ME_TO call. */
+	ipc_answer_0(iid, EOK);
+
+	while (1) {
+		callid = async_get_call(&call);
+		method = IPC_GET_METHOD(call);
+		switch (method) {
+		case IPC_M_PHONE_HUNGUP:
+			/* The other side has hung up. */
+			ipc_answer_0(callid, EOK);
+			return;
+		case IPC_M_CONNECT_TO_ME:
+			printf(NAME ": creating callback connection\n");
+			uart->client_phone = IPC_GET_ARG5(call);
+			retval = 0;
+			break;
+		case CHAR_WRITE_BYTE:
+			printf(NAME ": write %d to device\n",
+			    IPC_GET_ARG1(call));
+			s3c24xx_uart_sendb(uart, (uint8_t) IPC_GET_ARG1(call));
+			retval = 0;
+			break;
+		default:
+			retval = EINVAL;
+			break;
+		}
+		ipc_answer_0(callid, retval);
+	}
+}
+
+static void s3c24xx_uart_irq_handler(ipc_callid_t iid, ipc_call_t *call)
+{
+	(void) iid; (void) call;
+
+	while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_RX_COUNT) != 0) {
+		uint32_t data = pio_read_32(&uart->io->urxh) & 0xff;
+		uint32_t status = pio_read_32(&uart->io->uerstat);
+
+		if (uart->client_phone != -1) {
+			async_msg_1(uart->client_phone, CHAR_NOTIF_BYTE,
+			    data);
+		}
+
+		if (status != 0)
+			printf(NAME ": Error status 0x%x\n", status);
+	}
+}
+
+/** Initialize S3C24xx on-chip UART. */
+static int s3c24xx_uart_init(s3c24xx_uart_t *uart)
+{
+	void *vaddr;
+	sysarg_t inr;
+
+	if (sysinfo_get_value("s3c24xx_uart.address.physical",
+	    &uart->paddr) != EOK)
+		return -1;
+
+	if (pio_enable((void *) uart->paddr, sizeof(s3c24xx_uart_io_t),
+	    &vaddr) != 0)
+		return -1;
+
+	if (sysinfo_get_value("s3c24xx_uart.inr", &inr) != EOK)
+		return -1;
+
+	uart->io = vaddr;
+	uart->client_phone = -1;
+
+	printf(NAME ": device at physical address 0x%x, inr %d.\n",
+	    uart->paddr, inr);
+
+	async_set_interrupt_received(s3c24xx_uart_irq_handler);
+
+	ipc_register_irq(inr, device_assign_devno(), 0, &uart_irq_code);
+
+	/* Enable FIFO, Tx trigger level: empty, Rx trigger level: 1 byte. */
+	pio_write_32(&uart->io->ufcon, UFCON_FIFO_ENABLE |
+	    UFCON_TX_FIFO_TLEVEL_EMPTY | UFCON_RX_FIFO_TLEVEL_1B);
+
+	/* Set RX interrupt to pulse mode */
+	pio_write_32(&uart->io->ucon,
+	    pio_read_32(&uart->io->ucon) & ~UCON_RX_INT_LEVEL);
+
+	return EOK;
+}
+
+/** Send a byte to the UART. */
+static void s3c24xx_uart_sendb(s3c24xx_uart_t *uart, uint8_t byte)
+{
+	/* Wait for space becoming available in Tx FIFO. */
+	while ((pio_read_32(&uart->io->ufstat) & S3C24XX_UFSTAT_TX_FULL) != 0)
+		;
+
+	pio_write_32(&uart->io->utxh, byte);
+}
+
+/** @}
+ */
Index: uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.h
===================================================================
--- uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
+++ uspace/srv/hw/char/s3c24xx_uart/s3c24xx_uart.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2010 Jiri Svoboda
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup genarch
+ * @{
+ */
+/**
+ * @file
+ * @brief Samsung S3C24xx on-chip UART driver.
+ */
+
+#ifndef S3C24XX_UART_H_
+#define S3C24XX_UART_H_
+
+#include <sys/types.h>
+
+/** S3C24xx UART I/O */
+typedef struct {
+	uint32_t ulcon;
+	uint32_t ucon;
+	uint32_t ufcon;
+	uint32_t umcon;
+
+	uint32_t utrstat;
+	uint32_t uerstat;
+	uint32_t ufstat;
+	uint32_t umstat;
+
+	uint32_t utxh;
+	uint32_t urxh;
+
+	uint32_t ubrdiv;
+} s3c24xx_uart_io_t;
+
+/* Bits in UTRSTAT register */
+#define S3C24XX_UTRSTAT_TX_EMPTY	0x4
+#define S3C24XX_UTRSTAT_RDATA		0x1
+
+/* Bits in UFSTAT register */
+#define S3C24XX_UFSTAT_TX_FULL		0x4000
+#define S3C24XX_UFSTAT_RX_FULL		0x0040
+#define S3C24XX_UFSTAT_RX_COUNT		0x002f
+
+/* Bits in UCON register */
+#define UCON_RX_INT_LEVEL		0x100
+
+/* Bits in UFCON register */
+#define UFCON_TX_FIFO_TLEVEL_EMPTY	0x00
+#define UFCON_RX_FIFO_TLEVEL_1B		0x00
+#define UFCON_FIFO_ENABLE		0x01
+
+
+/** S3C24xx UART instance */
+typedef struct {
+	/** Physical device address */
+	uintptr_t paddr;
+
+	/** Device I/O structure */
+	s3c24xx_uart_io_t *io;
+
+	/** Callback phone to the client */
+	int client_phone;
+
+	/** Device handle */
+	dev_handle_t dev_handle;
+} s3c24xx_uart_t;
+
+#endif
+
+/** @}
+ */
Index: uspace/srv/hw/netif/dp8390/dp8390_module.c
===================================================================
--- uspace/srv/hw/netif/dp8390/dp8390_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/hw/netif/dp8390/dp8390_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -39,9 +39,9 @@
 #include <ddi.h>
 #include <errno.h>
+#include <err.h>
 #include <malloc.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/il/arp/arp.c
===================================================================
--- uspace/srv/net/il/arp/arp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/il/arp/arp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -45,6 +45,6 @@
 #include <ipc/ipc.h>
 #include <ipc/services.h>
-
-#include <net_err.h>
+#include <err.h>
+
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/il/arp/arp_module.c
===================================================================
--- uspace/srv/net/il/arp/arp_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/il/arp/arp_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,9 +40,9 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_interface.h>
Index: uspace/srv/net/il/ip/ip.c
===================================================================
--- uspace/srv/net/il/ip/ip.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/il/ip/ip.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,4 +38,5 @@
 #include <async.h>
 #include <errno.h>
+#include <err.h>
 #include <fibril_synch.h>
 #include <stdio.h>
@@ -45,5 +46,4 @@
 #include <sys/types.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
@@ -65,5 +65,4 @@
 #include <tl_interface.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <adt/measured_strings.h>
 #include <adt/module_map.h>
Index: uspace/srv/net/il/ip/ip_module.c
===================================================================
--- uspace/srv/net/il/ip/ip_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/il/ip/ip_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,6 +42,6 @@
 #include <ipc/ipc.h>
 #include <ipc/services.h>
+#include <err.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_interface.h>
Index: uspace/srv/net/net/net.c
===================================================================
--- uspace/srv/net/net/net.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/net/net.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,4 +40,5 @@
 #include <ddi.h>
 #include <errno.h>
+#include <err.h>
 #include <malloc.h>
 #include <stdio.h>
@@ -47,5 +48,4 @@
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/netif/lo/lo.c
===================================================================
--- uspace/srv/net/netif/lo/lo.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/netif/lo/lo.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -37,4 +37,5 @@
 #include <async.h>
 #include <errno.h>
+#include <err.h>
 #include <stdio.h>
 #include <str.h>
@@ -43,5 +44,4 @@
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/netstart/netstart.c
===================================================================
--- uspace/srv/net/netstart/netstart.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/netstart/netstart.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -45,8 +45,8 @@
 #include <task.h>
 #include <str_error.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_net_messages.h>
Index: uspace/srv/net/nil/eth/eth.c
===================================================================
--- uspace/srv/net/nil/eth/eth.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/nil/eth/eth.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,9 +41,9 @@
 #include <stdio.h>
 #include <str.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/nil/eth/eth_module.c
===================================================================
--- uspace/srv/net/nil/eth/eth_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/nil/eth/eth_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,9 +38,9 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_interface.h>
Index: uspace/srv/net/nil/nildummy/nildummy.c
===================================================================
--- uspace/srv/net/nil/nildummy/nildummy.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/nil/nildummy/nildummy.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -41,8 +41,8 @@
 #include <stdio.h>
 #include <str.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
Index: uspace/srv/net/nil/nildummy/nildummy_module.c
===================================================================
--- uspace/srv/net/nil/nildummy/nildummy_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/nil/nildummy/nildummy_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -38,9 +38,9 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <net_interface.h>
Index: uspace/srv/net/tl/icmp/icmp.c
===================================================================
--- uspace/srv/net/tl/icmp/icmp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/icmp/icmp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -46,6 +46,7 @@
 #include <sys/time.h>
 #include <sys/types.h>
-
-#include <net_err.h>
+#include <errno.h>
+#include <err.h>
+
 #include <net_messages.h>
 #include <net_modules.h>
@@ -66,5 +67,4 @@
 #include <net_interface.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <tl_messages.h>
 #include <tl_interface.h>
Index: uspace/srv/net/tl/icmp/icmp_module.c
===================================================================
--- uspace/srv/net/tl/icmp/icmp_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/icmp/icmp_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,8 +40,8 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <packet/packet.h>
Index: uspace/srv/net/tl/tcp/tcp.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/tcp/tcp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,9 +42,10 @@
 //TODO remove stdio
 #include <stdio.h>
+#include <errno.h>
+#include <err.h>
 
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_messages.h>
 #include <net_modules.h>
@@ -63,5 +64,4 @@
 #include <net_interface.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <tcp_codes.h>
 #include <socket_core.h>
@@ -76,49 +76,40 @@
 #include "tcp_module.h"
 
-/** TCP module name.
- */
+/** TCP module name. */
 #define NAME	"TCP protocol"
 
-/** The TCP window default value.
- */
-#define NET_DEFAULT_TCP_WINDOW	10240
-
-/** Initial timeout for new connections.
- */
+/** The TCP window default value. */
+#define NET_DEFAULT_TCP_WINDOW		10240
+
+/** Initial timeout for new connections. */
 #define NET_DEFAULT_TCP_INITIAL_TIMEOUT	3000000L
 
-/** Default timeout for closing.
- */
-#define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT	2000L
-
-/** The initial outgoing sequence number.
- */
-#define TCP_INITIAL_SEQUENCE_NUMBER		2999
-
-/** Maximum TCP fragment size.
- */
-#define MAX_TCP_FRAGMENT_SIZE	65535
-
-/** Free ports pool start.
- */
-#define TCP_FREE_PORTS_START	1025
-
-/** Free ports pool end.
- */
+/** Default timeout for closing. */
+#define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L
+
+/** The initial outgoing sequence number. */
+#define TCP_INITIAL_SEQUENCE_NUMBER	2999
+
+/** Maximum TCP fragment size. */
+#define MAX_TCP_FRAGMENT_SIZE		65535
+
+/** Free ports pool start. */
+#define TCP_FREE_PORTS_START		1025
+
+/** Free ports pool end. */
 #define TCP_FREE_PORTS_END		65535
 
-/** Timeout for connection initialization, SYN sent.
- */
-#define TCP_SYN_SENT_TIMEOUT	1000000L
-
-/** The maximum number of timeouts in a row before singaling connection lost.
- */
+/** Timeout for connection initialization, SYN sent. */
+#define TCP_SYN_SENT_TIMEOUT		1000000L
+
+/** The maximum number of timeouts in a row before singaling connection lost. */
 #define TCP_MAX_TIMEOUTS		8
 
-/** The number of acknowledgements before retransmit.
- */
+/** The number of acknowledgements before retransmit. */
 #define TCP_FAST_RETRANSMIT_COUNT	3
 
-/** Returns a value indicating whether the value is in the interval respecting the possible overflow.
+/** Returns a value indicating whether the value is in the interval respecting
+ *  the possible overflow.
+ *
  *  The high end and/or the value may overflow, be lower than the low value.
  *  @param[in] lower The last value before the interval.
@@ -126,15 +117,18 @@
  *  @param[in] higher_equal The last value in the interval.
  */
-#define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal)	((((lower) < (value)) && (((value) <= (higher_equal)) || ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && ((higher_equal) < (lower))))
+#define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \
+	((((lower) < (value)) && (((value) <= (higher_equal)) || \
+	((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && \
+	((higher_equal) < (lower))))
 
 /** Type definition of the TCP timeout.
  *  @see tcp_timeout
  */
-typedef struct tcp_timeout	tcp_timeout_t;
+typedef struct tcp_timeout tcp_timeout_t;
 
 /** Type definition of the TCP timeout pointer.
  *  @see tcp_timeout
  */
-typedef tcp_timeout_t *	tcp_timeout_ref;
+typedef tcp_timeout_t *tcp_timeout_ref;
 
 /** TCP reply timeout data.
@@ -142,31 +136,30 @@
  *  @see tcp_timeout()
  */
-struct tcp_timeout{
-	/** TCP global data are going to be read only.
-	 */
+struct tcp_timeout {
+	/** TCP global data are going to be read only. */
 	int globals_read_only;
-	/** Socket port.
-	 */
+
+	/** Socket port. */
 	int port;
-	/** Local sockets.
-	 */
+
+	/** Local sockets. */
 	socket_cores_ref local_sockets;
-	/** Socket identifier.
-	 */
+
+	/** Socket identifier. */
 	int socket_id;
-	/** Socket state.
-	 */
+
+	/** Socket state. */
 	tcp_socket_state_t state;
-	/** Sent packet sequence number.
-	 */
+
+	/** Sent packet sequence number. */
 	int sequence_number;
-	/** Timeout in microseconds.
-	 */
+
+	/** Timeout in microseconds. */
 	suseconds_t timeout;
-	/** Port map key.
-	 */
-	char * key;
-	/** Port map key length.
-	 */
+
+	/** Port map key. */
+	char *key;
+
+	/** Port map key length. */
 	size_t key_length;
 };
@@ -179,72 +172,121 @@
 int tcp_release_and_return(packet_t packet, int result);
 
-void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize);
-int tcp_prepare_timeout(int (*timeout_function)(void * tcp_timeout_t), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only);
+void tcp_prepare_operation_header(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
+    int finalize);
+int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
+    socket_core_ref socket, tcp_socket_data_ref socket_data,
+    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
+    int globals_read_only);
 void tcp_free_socket_data(socket_core_ref socket);
-int tcp_timeout(void * data);
-int tcp_release_after_timeout(void * data);
-int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error);
-int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen);
-int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
-int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
-packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data);
+
+int tcp_timeout(void *data);
+
+int tcp_release_after_timeout(void *data);
+
+int tcp_process_packet(device_id_t device_id, packet_t packet,
+    services_t error);
+int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
+    struct sockaddr *addr, socklen_t addrlen);
+int tcp_queue_prepare_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length);
+int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    packet_t packet, size_t data_length);
+packet_t tcp_get_packets_to_send(socket_core_ref socket,
+    tcp_socket_data_ref socket_data);
 void tcp_send_packets(device_id_t device_id, packet_t packet);
-void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header);
-packet_t tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number);
-packet_t tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number);
-void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number);
-int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize);
+
+void tcp_process_acknowledgement(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header);
+packet_t tcp_send_prepare_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length,
+    size_t sequence_number);
+packet_t tcp_prepare_copy(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length,
+    size_t sequence_number);
+void tcp_retransmit_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, size_t sequence_number);
+int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket,
+    tcp_socket_data_ref socket_data, int synchronize, int finalize);
 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data);
+
 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data);
-int tcp_process_listen(socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen);
-int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
-int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
-int tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length);
-int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length);
-
-int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error);
+
+int tcp_process_listen(socket_core_ref listening_socket,
+    tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
+    packet_t packet, struct sockaddr *src, struct sockaddr *dest,
+    size_t addrlen);
+int tcp_process_syn_sent(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
+int tcp_process_syn_received(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet);
+int tcp_process_established(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet,
+    int fragments, size_t total_length);
+int tcp_queue_received_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, int fragments,
+    size_t total_length);
+
+int tcp_received_msg(device_id_t device_id, packet_t packet,
+    services_t receiver, services_t error);
 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
-int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog);
-int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen);
-int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen);
-int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags);
-int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen);
+
+int tcp_listen_message(socket_cores_ref local_sockets, int socket_id,
+    int backlog);
+int tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
+    struct sockaddr *addr, socklen_t addrlen);
+int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id,
+    int flags, size_t * addrlen);
+int tcp_send_message(socket_cores_ref local_sockets, int socket_id,
+    int fragments, size_t * data_fragment_size, int flags);
+int tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
+    int new_socket_id, size_t * data_fragment_size, size_t * addrlen);
 int tcp_close_message(socket_cores_ref local_sockets, int socket_id);
 
-/** TCP global data.
- */
-tcp_globals_t	tcp_globals;
-
-int tcp_initialize(async_client_conn_t client_connection){
+/** TCP global data. */
+tcp_globals_t tcp_globals;
+
+int tcp_initialize(async_client_conn_t client_connection)
+{
 	ERROR_DECLARE;
 
 	assert(client_connection);
+
 	fibril_rwlock_initialize(&tcp_globals.lock);
 	fibril_rwlock_write_lock(&tcp_globals.lock);
-	tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
-	tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg);
-	if(tcp_globals.ip_phone < 0){
+
+	tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
+	    ICMP_CONNECT_TIMEOUT);
+	tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
+	    SERVICE_TCP, client_connection, tcp_received_msg);
+	if (tcp_globals.ip_phone < 0)
 		return tcp_globals.ip_phone;
-	}
+	
 	ERROR_PROPAGATE(socket_ports_initialize(&tcp_globals.sockets));
-	if(ERROR_OCCURRED(packet_dimensions_initialize(&tcp_globals.dimensions))){
+	if (ERROR_OCCURRED(packet_dimensions_initialize(
+	    &tcp_globals.dimensions))) {
 		socket_ports_destroy(&tcp_globals.sockets);
 		return ERROR_CODE;
 	}
+
 	tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
 	fibril_rwlock_write_unlock(&tcp_globals.lock);
+
 	return EOK;
 }
 
-int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){
+int
+tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
+    services_t error)
+{
 	ERROR_DECLARE;
 
-	if(receiver != SERVICE_TCP){
+	if (receiver != SERVICE_TCP)
 		return EREFUSED;
-	}
+
 	fibril_rwlock_write_lock(&tcp_globals.lock);
-	if(ERROR_OCCURRED(tcp_process_packet(device_id, packet, error))){
+	if (ERROR_OCCURRED(tcp_process_packet(device_id, packet, error)))
 		fibril_rwlock_write_unlock(&tcp_globals.lock);
-	}
+
 	printf("receive %d \n", ERROR_CODE);
 
@@ -252,5 +294,6 @@
 }
 
-int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error){
+int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error)
+{
 	ERROR_DECLARE;
 
@@ -259,5 +302,5 @@
 	int result;
 	tcp_header_ref header;
-	socket_core_ref  socket;
+	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
 	packet_t next_packet;
@@ -267,24 +310,26 @@
 	icmp_type_t type;
 	icmp_code_t code;
-	struct sockaddr * src;
-	struct sockaddr * dest;
+	struct sockaddr *src;
+	struct sockaddr *dest;
 	size_t addrlen;
 
-	printf("p1 \n");
-	if(error){
-		switch(error){
-			case SERVICE_ICMP:
-				// process error
-				result = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
-				if(result < 0){
-					return tcp_release_and_return(packet, result);
-				}
-				length = (size_t) result;
-				if(ERROR_OCCURRED(packet_trim(packet, length, 0))){
-					return tcp_release_and_return(packet, ERROR_CODE);
-				}
-				break;
-			default:
-				return tcp_release_and_return(packet, ENOTSUP);
+	if (error) {
+		switch (error) {
+		case SERVICE_ICMP:
+			// process error
+			result = icmp_client_process_packet(packet, &type,
+			    &code, NULL, NULL);
+			if (result < 0)
+				return tcp_release_and_return(packet, result);
+
+			length = (size_t) result;
+			if (ERROR_OCCURRED(packet_trim(packet, length, 0))) {
+				return tcp_release_and_return(packet,
+				    ERROR_CODE);
+			}
+			break;
+
+		default:
+			return tcp_release_and_return(packet, ENOTSUP);
 		}
 	}
@@ -292,50 +337,52 @@
 	// TODO process received ipopts?
 	result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
-//	printf("ip len %d\n", result);
-	if(result < 0){
+	if (result < 0)
 		return tcp_release_and_return(packet, result);
-	}
+
 	offset = (size_t) result;
 
 	length = packet_get_data_length(packet);
-//	printf("packet len %d\n", length);
-	if(length <= 0){
+	if (length <= 0)
 		return tcp_release_and_return(packet, EINVAL);
-	}
-	if(length < TCP_HEADER_SIZE + offset){
+
+	if (length < TCP_HEADER_SIZE + offset)
 		return tcp_release_and_return(packet, NO_DATA);
-	}
 
 	// trim all but TCP header
-	if(ERROR_OCCURRED(packet_trim(packet, offset, 0))){
+	if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
 		return tcp_release_and_return(packet, ERROR_CODE);
-	}
 
 	// get tcp header
 	header = (tcp_header_ref) packet_get_data(packet);
-	if(! header){
+	if (!header)
 		return tcp_release_and_return(packet, NO_DATA);
-	}
-//	printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), ntohs(header->destination_port));
+
+//      printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header),
+//	    ntohs(header->destination_port));
 
 	result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
-	if(result <= 0){
+	if (result <= 0)
 		return tcp_release_and_return(packet, result);
-	}
+
 	addrlen = (size_t) result;
 
-	if(ERROR_OCCURRED(tl_set_address_port(src, addrlen, ntohs(header->source_port)))){
+	if (ERROR_OCCURRED(tl_set_address_port(src, addrlen,
+	    ntohs(header->source_port)))) 
 		return tcp_release_and_return(packet, ERROR_CODE);
-	}
-
+	
 	// find the destination socket
-	socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), (const char *) src, addrlen);
-	if(! socket){
-//		printf("listening?\n");
+	socket = socket_port_find(&tcp_globals.sockets,
+	    ntohs(header->destination_port), (const char *) src, addrlen);
+	if (!socket) {
 		// find the listening destination socket
-		socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
-		if(! socket){
-			if(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error) == EOK){
-				icmp_destination_unreachable_msg(tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet);
+		socket = socket_port_find(&tcp_globals.sockets,
+		    ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,
+		    0);
+		if (!socket) {
+			if (tl_prepare_icmp_packet(tcp_globals.net_phone,
+			    tcp_globals.icmp_phone, packet, error) == EOK) {
+				icmp_destination_unreachable_msg(
+				    tcp_globals.icmp_phone, ICMP_PORT_UNREACH,
+				    0, packet);
 			}
 			return EADDRNOTAVAIL;
@@ -354,85 +401,113 @@
 	checksum = 0;
 	total_length = 0;
-	do{
-		++ fragments;
+	do {
+		++fragments;
 		length = packet_get_data_length(next_packet);
-		if(length <= 0){
+		if (length <= 0)
 			return tcp_release_and_return(packet, NO_DATA);
-		}
+
 		total_length += length;
+
 		// add partial checksum if set
-		if(! error){
-			checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
-		}
-	}while((next_packet = pq_next(next_packet)));
-//	printf("fragments %d of %d bytes\n", fragments, total_length);
-
-//	printf("lock?\n");
+		if (!error) {
+			checksum = compute_checksum(checksum,
+			    packet_get_data(packet),
+			    packet_get_data_length(packet));
+		}
+
+	} while ((next_packet = pq_next(next_packet)));
+
 	fibril_rwlock_write_lock(socket_data->local_lock);
-//	printf("locked\n");
-	if(! error){
-		if(socket_data->state == TCP_SOCKET_LISTEN){
-			if(socket_data->pseudo_header){
-				free(socket_data->pseudo_header);
-				socket_data->pseudo_header = NULL;
-				socket_data->headerlen = 0;
-			}
-			if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, &socket_data->pseudo_header, &socket_data->headerlen))){
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
-		}else if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, total_length))){
+
+	if (error)
+		goto error;
+	
+	if (socket_data->state == TCP_SOCKET_LISTEN) {
+
+		if (socket_data->pseudo_header) {
+			free(socket_data->pseudo_header);
+			socket_data->pseudo_header = NULL;
+			socket_data->headerlen = 0;
+		}
+
+		if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src,
+		    addrlen, dest, addrlen, total_length,
+		    &socket_data->pseudo_header, &socket_data->headerlen))) {
 			fibril_rwlock_write_unlock(socket_data->local_lock);
 			return tcp_release_and_return(packet, ERROR_CODE);
 		}
-		checksum = compute_checksum(checksum, socket_data->pseudo_header, socket_data->headerlen);
-		if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){
-			printf("checksum err %x -> %x\n", header->checksum, flip_checksum(compact_checksum(checksum)));
-			fibril_rwlock_write_unlock(socket_data->local_lock);
-			if(! ERROR_OCCURRED(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error))){
-				// checksum error ICMP
-				icmp_parameter_problem_msg(tcp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet);
-			}
-			return EINVAL;
-		}
-	}
-
+
+	} else if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
+	    socket_data->pseudo_header, socket_data->headerlen,
+	    total_length))) {
+		fibril_rwlock_write_unlock(socket_data->local_lock);
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+	
+	checksum = compute_checksum(checksum, socket_data->pseudo_header,
+	    socket_data->headerlen);
+	if (flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO) {
+		printf("checksum err %x -> %x\n", header->checksum,
+		    flip_checksum(compact_checksum(checksum)));
+		fibril_rwlock_write_unlock(socket_data->local_lock);
+
+		if (ERROR_NONE(tl_prepare_icmp_packet(tcp_globals.net_phone,
+		    tcp_globals.icmp_phone, packet, error))) {
+			// checksum error ICMP
+			icmp_parameter_problem_msg(tcp_globals.icmp_phone,
+			    ICMP_PARAM_POINTER,
+			    ((size_t) ((void *) &header->checksum)) -
+			    ((size_t) ((void *) header)), packet);
+		}
+
+		return EINVAL;
+	}
+
+error:
 	fibril_rwlock_read_unlock(&tcp_globals.lock);
 
 	// TODO error reporting/handling
-//	printf("st %d\n", socket_data->state);
-	switch(socket_data->state){
-		case TCP_SOCKET_LISTEN:
-			ERROR_CODE = tcp_process_listen(socket, socket_data, header, packet, src, dest, addrlen);
-			break;
-		case TCP_SOCKET_SYN_RECEIVED:
-			ERROR_CODE = tcp_process_syn_received(socket, socket_data, header, packet);
-			break;
-		case TCP_SOCKET_SYN_SENT:
-			ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header, packet);
-			break;
-		case TCP_SOCKET_FIN_WAIT_1:
-			// ack changing the state to FIN_WAIT_2 gets processed later
-		case TCP_SOCKET_FIN_WAIT_2:
-			// fin changing state to LAST_ACK gets processed later
-		case TCP_SOCKET_LAST_ACK:
-			// ack releasing the socket get processed later
-		case TCP_SOCKET_CLOSING:
-			// ack releasing the socket gets processed later
-		case TCP_SOCKET_ESTABLISHED:
-			ERROR_CODE = tcp_process_established(socket, socket_data, header, packet, fragments, total_length);
-			break;
-		default:
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-	}
-
-	if(ERROR_CODE != EOK){
+	switch (socket_data->state) {
+	case TCP_SOCKET_LISTEN:
+		ERROR_CODE = tcp_process_listen(socket, socket_data, header,
+		    packet, src, dest, addrlen);
+		break;
+	case TCP_SOCKET_SYN_RECEIVED:
+		ERROR_CODE = tcp_process_syn_received(socket, socket_data,
+		    header, packet);
+		break;
+	case TCP_SOCKET_SYN_SENT:
+		ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header,
+		    packet);
+		break;
+	case TCP_SOCKET_FIN_WAIT_1:
+		// ack changing the state to FIN_WAIT_2 gets processed later
+	case TCP_SOCKET_FIN_WAIT_2:
+		// fin changing state to LAST_ACK gets processed later
+	case TCP_SOCKET_LAST_ACK:
+		// ack releasing the socket get processed later
+	case TCP_SOCKET_CLOSING:
+		// ack releasing the socket gets processed later
+	case TCP_SOCKET_ESTABLISHED:
+		ERROR_CODE = tcp_process_established(socket, socket_data,
+		    header, packet, fragments, total_length);
+		break;
+	default:
+		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+	}
+
+	if (ERROR_CODE != EOK) {
 		printf("process %d\n", ERROR_CODE);
 		fibril_rwlock_write_unlock(socket_data->local_lock);
 	}
+
 	return EOK;
 }
 
-int tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length){
+int
+tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    tcp_header_ref header, packet_t packet, int fragments,
+    size_t total_length)
+{
 	ERROR_DECLARE;
 
@@ -455,60 +530,72 @@
 	old_incoming = socket_data->next_incoming;
 
-	if(header->finalize){
+	if (header->finalize)
 		socket_data->fin_incoming = new_sequence_number;
-	}
-
-//	printf("pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length);
+
 	// trim begining if containing expected data
-	if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length)){
+	if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
+	    socket_data->next_incoming, new_sequence_number + total_length)) {
+
 		// get the acknowledged offset
-		if(socket_data->next_incoming < new_sequence_number){
-			offset = new_sequence_number - socket_data->next_incoming;
-		}else{
-			offset = socket_data->next_incoming - new_sequence_number;
-		}
-//		printf("offset %d\n", offset);
+		if (socket_data->next_incoming < new_sequence_number) {
+			offset = new_sequence_number -
+			    socket_data->next_incoming;
+		} else {
+			offset = socket_data->next_incoming -
+			    new_sequence_number;
+		}
+
 		new_sequence_number += offset;
 		total_length -= offset;
 		length = packet_get_data_length(packet);
 		// trim the acknowledged data
-		while(length <= offset){
+		while (length <= offset) {
 			// release the acknowledged packets
 			next_packet = pq_next(packet);
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(packet));
 			packet = next_packet;
 			offset -= length;
 			length = packet_get_data_length(packet);
 		}
-		if((offset > 0)
-			&& (ERROR_OCCURRED(packet_trim(packet, offset, 0)))){
+
+		if ((offset > 0) && (ERROR_OCCURRED(packet_trim(packet,
+		    offset, 0))))
 			return tcp_release_and_return(packet, ERROR_CODE);
-		}
+
 		assert(new_sequence_number == socket_data->next_incoming);
 	}
 
 	// release if overflowing the window
-//	if(IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming + socket_data->window, new_sequence_number, new_sequence_number + total_length)){
-//		return tcp_release_and_return(packet, EOVERFLOW);
-//	}
-
 /*
+	if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +
+	    socket_data->window, new_sequence_number, new_sequence_number +
+	    total_length)) {
+		return tcp_release_and_return(packet, EOVERFLOW);
+	}
+
 	// trim end if overflowing the window
-	if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming + socket_data->window, new_sequence_number + total_length)){
+	if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
+	    socket_data->next_incoming + socket_data->window,
+	    new_sequence_number + total_length)) {
 		// get the allowed data length
-		if(socket_data->next_incoming + socket_data->window < new_sequence_number){
-			offset = new_sequence_number - socket_data->next_incoming + socket_data->window;
-		}else{
-			offset = socket_data->next_incoming + socket_data->window - new_sequence_number;
+		if (socket_data->next_incoming + socket_data->window <
+		    new_sequence_number) {
+			offset = new_sequence_number -
+			    socket_data->next_incoming + socket_data->window;
+		} else {
+			offset = socket_data->next_incoming +
+			    socket_data->window - new_sequence_number;
 		}
 		next_packet = packet;
 		// trim the overflowing data
-		while(next_packet && (offset > 0)){
+		while (next_packet && (offset > 0)) {
 			length = packet_get_data_length(packet);
-			if(length <= offset){
+			if (length <= offset)
 				next_packet = pq_next(next_packet);
-			}else if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - offset))){
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
+			else if (ERROR_OCCURRED(packet_trim(next_packet, 0,
+			    length - offset)))
+				return tcp_release_and_return(packet,
+				    ERROR_CODE);
 			offset -= length;
 			total_length -= length - offset;
@@ -516,15 +603,17 @@
 		// release the overflowing packets
 		next_packet = pq_next(next_packet);
-		if(next_packet){
+		if (next_packet) {
 			tmp_packet = next_packet;
 			next_packet = pq_next(next_packet);
 			pq_insert_after(tmp_packet, next_packet);
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(tmp_packet));
-		}
-		assert(new_sequence_number + total_length == socket_data->next_incoming + socket_data->window);
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(tmp_packet));
+		}
+		assert(new_sequence_number + total_length ==
+		    socket_data->next_incoming + socket_data->window);
 	}
 */
 	// the expected one arrived?
-	if(new_sequence_number == socket_data->next_incoming){
+	if (new_sequence_number == socket_data->next_incoming) {
 		printf("expected\n");
 		// process acknowledgement
@@ -533,64 +622,88 @@
 		// remove the header
 		total_length -= TCP_HEADER_LENGTH(header);
-		if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){
+		if (ERROR_OCCURRED(packet_trim(packet,
+		    TCP_HEADER_LENGTH(header), 0)))
 			return tcp_release_and_return(packet, ERROR_CODE);
-		}
-
-		if(total_length){
-			ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, fragments, total_length));
-		}else{
+
+		if (total_length) {
+			ERROR_PROPAGATE(tcp_queue_received_packet(socket,
+			    socket_data, packet, fragments, total_length));
+		} else {
 			total_length = 1;
 		}
+
 		socket_data->next_incoming = old_incoming + total_length;
 		packet = socket_data->incoming;
-		while(packet){
-			if(ERROR_OCCURRED(pq_get_order(socket_data->incoming, &order, NULL))){
+		while (packet) {
+
+			if (ERROR_OCCURRED(pq_get_order(socket_data->incoming,
+			    &order, NULL))) {
 				// remove the corrupted packet
 				next_packet = pq_detach(packet);
-				if(packet == socket_data->incoming){
+				if (packet == socket_data->incoming)
 					socket_data->incoming = next_packet;
-				}
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+				pq_release_remote(tcp_globals.net_phone,
+				    packet_get_id(packet));
 				packet = next_packet;
 				continue;
 			}
+
 			sequence_number = (uint32_t) order;
-			if(IS_IN_INTERVAL_OVERFLOW(sequence_number, old_incoming, socket_data->next_incoming)){
+			if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
+			    old_incoming, socket_data->next_incoming)) {
 				// move to the next
 				packet = pq_next(packet);
-			// coninual data?
-			}else if(IS_IN_INTERVAL_OVERFLOW(old_incoming, sequence_number, socket_data->next_incoming)){
+				// coninual data?
+			} else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
+			    sequence_number, socket_data->next_incoming)) {
 				// detach the packet
 				next_packet = pq_detach(packet);
-				if(packet == socket_data->incoming){
+				if (packet == socket_data->incoming)
 					socket_data->incoming = next_packet;
-				}
 				// get data length
 				length = packet_get_data_length(packet);
 				new_sequence_number = sequence_number + length;
-				if(length <= 0){
+				if (length <= 0) {
 					// remove the empty packet
-					pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+					pq_release_remote(tcp_globals.net_phone,
+					    packet_get_id(packet));
 					packet = next_packet;
 					continue;
 				}
 				// exactly following
-				if(sequence_number == socket_data->next_incoming){
+				if (sequence_number ==
+				    socket_data->next_incoming) {
 					// queue received data
-					ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet)));
-					socket_data->next_incoming = new_sequence_number;
+					ERROR_PROPAGATE(
+					    tcp_queue_received_packet(socket,
+					    socket_data, packet, 1,
+					    packet_get_data_length(packet)));
+					socket_data->next_incoming =
+					    new_sequence_number;
 					packet = next_packet;
 					continue;
-				// at least partly following data?
-				}else if(IS_IN_INTERVAL_OVERFLOW(sequence_number, socket_data->next_incoming, new_sequence_number)){
-					if(socket_data->next_incoming < new_sequence_number){
-						length = new_sequence_number - socket_data->next_incoming;
-					}else{
-						length = socket_data->next_incoming - new_sequence_number;
+					// at least partly following data?
+				} else if (IS_IN_INTERVAL_OVERFLOW(
+				    sequence_number, socket_data->next_incoming,
+				    new_sequence_number)) {
+					if (socket_data->next_incoming <
+					    new_sequence_number) {
+						length = new_sequence_number -
+						    socket_data->next_incoming;
+					} else {
+						length =
+						    socket_data->next_incoming -
+						    new_sequence_number;
 					}
-					if(! ERROR_OCCURRED(packet_trim(packet, length, 0))){
+					if (ERROR_NONE(packet_trim(packet,
+					    length, 0))) {
 						// queue received data
-						ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet)));
-						socket_data->next_incoming = new_sequence_number;
+						ERROR_PROPAGATE(
+						    tcp_queue_received_packet(
+						    socket, socket_data, packet,
+						    1, packet_get_data_length(
+						    packet)));
+						socket_data->next_incoming =
+						    new_sequence_number;
 						packet = next_packet;
 						continue;
@@ -598,12 +711,15 @@
 				}
 				// remove the duplicit or corrupted packet
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+				pq_release_remote(tcp_globals.net_phone,
+				    packet_get_id(packet));
 				packet = next_packet;
 				continue;
-			}else{
+			} else {
 				break;
 			}
 		}
-	}else if(IS_IN_INTERVAL(socket_data->next_incoming, new_sequence_number, socket_data->next_incoming + socket_data->window)){
+	} else if (IS_IN_INTERVAL(socket_data->next_incoming,
+	    new_sequence_number,
+	    socket_data->next_incoming + socket_data->window)) {
 		printf("in window\n");
 		// process acknowledgement
@@ -612,27 +728,33 @@
 		// remove the header
 		total_length -= TCP_HEADER_LENGTH(header);
-		if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){
+		if (ERROR_OCCURRED(packet_trim(packet,
+		    TCP_HEADER_LENGTH(header), 0)))
 			return tcp_release_and_return(packet, ERROR_CODE);
-		}
 
 		next_packet = pq_detach(packet);
 		length = packet_get_data_length(packet);
-		if(ERROR_OCCURRED(pq_add(&socket_data->incoming, packet, new_sequence_number, length))){
+		if (ERROR_OCCURRED(pq_add(&socket_data->incoming, packet,
+		    new_sequence_number, length))) {
 			// remove the corrupted packets
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
-		}else{
-			while(next_packet){
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(packet));
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(next_packet));
+		} else {
+			while (next_packet) {
 				new_sequence_number += length;
 				tmp_packet = pq_detach(next_packet);
 				length = packet_get_data_length(next_packet);
-				if(ERROR_OCCURRED(pq_set_order(next_packet, new_sequence_number, length))
-					|| ERROR_OCCURRED(pq_insert_after(packet, next_packet))){
-					pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
+				if (ERROR_OCCURRED(pq_set_order(next_packet,
+				    new_sequence_number, length)) ||
+				    ERROR_OCCURRED(pq_insert_after(packet,
+				    next_packet))) {
+					pq_release_remote(tcp_globals.net_phone,
+					    packet_get_id(next_packet));
 				}
 				next_packet = tmp_packet;
 			}
 		}
-	}else{
+	} else {
 		printf("unexpected\n");
 		// release duplicite or restricted
@@ -641,32 +763,43 @@
 
 	// change state according to the acknowledging incoming fin
-	if(IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming, socket_data->next_incoming)){
-		switch(socket_data->state){
-			case TCP_SOCKET_FIN_WAIT_1:
-			case TCP_SOCKET_FIN_WAIT_2:
-			case TCP_SOCKET_CLOSING:
-				socket_data->state = TCP_SOCKET_CLOSING;
-				break;
+	if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming,
+	    socket_data->next_incoming)) {
+		switch (socket_data->state) {
+		case TCP_SOCKET_FIN_WAIT_1:
+		case TCP_SOCKET_FIN_WAIT_2:
+		case TCP_SOCKET_CLOSING:
+			socket_data->state = TCP_SOCKET_CLOSING;
+			break;
 			//case TCP_ESTABLISHED:
-			default:
-				socket_data->state = TCP_SOCKET_CLOSE_WAIT;
-				break;
+		default:
+			socket_data->state = TCP_SOCKET_CLOSE_WAIT;
+			break;
 		}
 	}
 
 	packet = tcp_get_packets_to_send(socket, socket_data);
-	if(! packet){
+	if (!packet) {
 		// create the notification packet
-		ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 0));
-		ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, 1));
-		packet = tcp_send_prepare_packet(socket, socket_data, packet, 1, socket_data->last_outgoing + 1);
-	}
+		ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+		    socket_data, 0, 0));
+		ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data,
+		    packet, 1));
+		packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
+		    socket_data->last_outgoing + 1);
+	}
+
 	fibril_rwlock_write_unlock(socket_data->local_lock);
+
 	// send the packet
 	tcp_send_packets(socket_data->device_id, packet);
+
 	return EOK;
 }
 
-int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length){
+int
+tcp_queue_received_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, int fragments,
+    size_t total_length)
+{
 	ERROR_DECLARE;
 
@@ -681,6 +814,9 @@
 
 	// queue the received packet
-	if(ERROR_OCCURRED(dyn_fifo_push(&socket->received, packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE))
-	    || ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension))){
+	if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
+	    packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
+	    ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id,
+	    &packet_dimension))) {
 		return tcp_release_and_return(packet, ERROR_CODE);
 	}
@@ -690,9 +826,17 @@
 
 	// notify the destination socket
-	async_msg_5(socket->phone, NET_SOCKET_RECEIVED, (ipcarg_t) socket->socket_id, ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size), 0, 0, (ipcarg_t) fragments);
+	async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
+	    (ipcarg_t) socket->socket_id,
+	    ((packet_dimension->content < socket_data->data_fragment_size) ?
+	    packet_dimension->content : socket_data->data_fragment_size), 0, 0,
+	    (ipcarg_t) fragments);
+
 	return EOK;
 }
 
-int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){
+int
+tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    tcp_header_ref header, packet_t packet)
+{
 	ERROR_DECLARE;
 
@@ -705,41 +849,50 @@
 	assert(packet);
 
-	if(header->synchronize){
-		// process acknowledgement
-		tcp_process_acknowledgement(socket, socket_data, header);
-
-		socket_data->next_incoming = ntohl(header->sequence_number) + 1;
-		// release additional packets
-		next_packet = pq_detach(packet);
-		if(next_packet){
-			pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
-		}
-		// trim if longer than the header
-		if((packet_get_data_length(packet) > sizeof(*header))
-			&& ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - sizeof(*header)))){
-			return tcp_release_and_return(packet, ERROR_CODE);
-		}
-		tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
-		fibril_mutex_lock(&socket_data->operation.mutex);
-		socket_data->operation.result = tcp_queue_packet(socket, socket_data, packet, 1);
-		if(socket_data->operation.result == EOK){
-			socket_data->state = TCP_SOCKET_ESTABLISHED;
-			packet = tcp_get_packets_to_send(socket, socket_data);
-			if(packet){
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				// send the packet
-				tcp_send_packets(socket_data->device_id, packet);
-				// signal the result
-				fibril_condvar_signal(&socket_data->operation.condvar);
-				fibril_mutex_unlock(&socket_data->operation.mutex);
-				return EOK;
-			}
-		}
-		fibril_mutex_unlock(&socket_data->operation.mutex);
-	}
+	if (!header->synchronize)
+		return tcp_release_and_return(packet, EINVAL);
+	
+	// process acknowledgement
+	tcp_process_acknowledgement(socket, socket_data, header);
+
+	socket_data->next_incoming = ntohl(header->sequence_number) + 1;
+	// release additional packets
+	next_packet = pq_detach(packet);
+	if (next_packet) {
+		pq_release_remote(tcp_globals.net_phone,
+		    packet_get_id(next_packet));
+	}
+	// trim if longer than the header
+	if ((packet_get_data_length(packet) > sizeof(*header)) &&
+	    ERROR_OCCURRED(packet_trim(packet, 0,
+	    packet_get_data_length(packet) - sizeof(*header)))) {
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+	tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
+	fibril_mutex_lock(&socket_data->operation.mutex);
+	socket_data->operation.result = tcp_queue_packet(socket, socket_data,
+	    packet, 1);
+	if (socket_data->operation.result == EOK) {
+		socket_data->state = TCP_SOCKET_ESTABLISHED;
+		packet = tcp_get_packets_to_send(socket, socket_data);
+		if (packet) {
+			fibril_rwlock_write_unlock( socket_data->local_lock);
+			// send the packet
+			tcp_send_packets(socket_data->device_id, packet);
+			// signal the result
+			fibril_condvar_signal( &socket_data->operation.condvar);
+			fibril_mutex_unlock( &socket_data->operation.mutex);
+			return EOK;
+		}
+	}
+	fibril_mutex_unlock(&socket_data->operation.mutex);
 	return tcp_release_and_return(packet, EINVAL);
 }
 
-int tcp_process_listen(socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen){
+int
+tcp_process_listen(socket_core_ref listening_socket,
+    tcp_socket_data_ref listening_socket_data, tcp_header_ref header,
+    packet_t packet, struct sockaddr *src, struct sockaddr *dest,
+    size_t addrlen)
+{
 	ERROR_DECLARE;
 
@@ -757,128 +910,144 @@
 	assert(packet);
 
-//	printf("syn %d\n", header->synchronize);
-	if(header->synchronize){
-		socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
-		if(! socket_data){
-			return tcp_release_and_return(packet, ENOMEM);
-		}else{
-			tcp_initialize_socket_data(socket_data);
-			socket_data->local_lock = listening_socket_data->local_lock;
-			socket_data->local_sockets = listening_socket_data->local_sockets;
-			socket_data->listening_socket_id = listening_socket->socket_id;
-
-			socket_data->next_incoming = ntohl(header->sequence_number);
-			socket_data->treshold = socket_data->next_incoming + ntohs(header->window);
-
-			socket_data->addrlen = addrlen;
-			socket_data->addr = malloc(socket_data->addrlen);
-			if(! socket_data->addr){
-				free(socket_data);
-				return tcp_release_and_return(packet, ENOMEM);
-			}
-			memcpy(socket_data->addr, src, socket_data->addrlen);
-
-			socket_data->dest_port = ntohs(header->source_port);
-			if(ERROR_OCCURRED(tl_set_address_port(socket_data->addr, socket_data->addrlen, socket_data->dest_port))){
-				free(socket_data->addr);
-				free(socket_data);
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-				return ERROR_CODE;
-			}
-
-//			printf("addr %p\n", socket_data->addr, socket_data->addrlen);
-			// create a socket
-			socket_id = -1;
-			if(ERROR_OCCURRED(socket_create(socket_data->local_sockets, listening_socket->phone, socket_data, &socket_id))){
-				free(socket_data->addr);
-				free(socket_data);
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
-
-			printf("new_sock %d\n", socket_id);
-			socket_data->pseudo_header = listening_socket_data->pseudo_header;
-			socket_data->headerlen = listening_socket_data->headerlen;
-			listening_socket_data->pseudo_header = NULL;
-			listening_socket_data->headerlen = 0;
-
-			fibril_rwlock_write_unlock(socket_data->local_lock);
-//			printf("list lg\n");
-			fibril_rwlock_write_lock(&tcp_globals.lock);
-//			printf("list locked\n");
-			// find the destination socket
-			listening_socket = socket_port_find(&tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0);
-			if((! listening_socket) || (listening_socket->socket_id != listening_socket_id)){
-				fibril_rwlock_write_unlock(&tcp_globals.lock);
-				// a shadow may remain until app hangs up
-				return tcp_release_and_return(packet, EOK/*ENOTSOCK*/);
-			}
-//			printf("port %d\n", listening_socket->port);
-			listening_socket_data = (tcp_socket_data_ref) listening_socket->specific_data;
-			assert(listening_socket_data);
-
-//			printf("list ll\n");
-			fibril_rwlock_write_lock(listening_socket_data->local_lock);
-//			printf("list locked\n");
-
-			socket = socket_cores_find(listening_socket_data->local_sockets, socket_id);
-			if(! socket){
-				// where is the socket?!?
-				fibril_rwlock_write_unlock(&tcp_globals.lock);
-				return ENOTSOCK;
-			}
-			socket_data = (tcp_socket_data_ref) socket->specific_data;
-			assert(socket_data);
-
-//			uint8_t * data = socket_data->addr;
-//			printf("addr %d of %x %x %x %x-%x %x %x %x-%x %x %x %x-%x %x %x %x\n", socket_data->addrlen, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15]);
-
-			ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port, socket, (const char *) socket_data->addr, socket_data->addrlen);
-			assert(socket == socket_port_find(&tcp_globals.sockets, listening_port, (const char *) socket_data->addr, socket_data->addrlen));
-			//ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port);
-			//tcp_globals.last_used_port = socket->port;
-//			printf("bound %d\n", socket->port);
-			fibril_rwlock_write_unlock(&tcp_globals.lock);
-			if(ERROR_CODE != EOK){
-				socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
-
-			socket_data->state = TCP_SOCKET_LISTEN;
-			socket_data->next_incoming = ntohl(header->sequence_number) + 1;
-			// release additional packets
-			next_packet = pq_detach(packet);
-			if(next_packet){
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet));
-			}
-			// trim if longer than the header
-			if((packet_get_data_length(packet) > sizeof(*header))
-				&& ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - sizeof(*header)))){
-				socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
-				return tcp_release_and_return(packet, ERROR_CODE);
-			}
-			tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
-			if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))){
-				socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
-				return ERROR_CODE;
-			}
-			packet = tcp_get_packets_to_send(socket, socket_data);
-//			printf("send %d\n", packet_get_id(packet));
-			if(! packet){
-				socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
-				return EINVAL;
-			}else{
-				socket_data->state = TCP_SOCKET_SYN_RECEIVED;
-//				printf("unlock\n");
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				// send the packet
-				tcp_send_packets(socket_data->device_id, packet);
-				return EOK;
-			}
-		}
-	}
-	return tcp_release_and_return(packet, EINVAL);
-}
-
-int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){
+	if (!header->synchronize)
+		return tcp_release_and_return(packet, EINVAL);
+
+	socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
+	if (!socket_data)
+		return tcp_release_and_return(packet, ENOMEM);
+
+	tcp_initialize_socket_data(socket_data);
+	socket_data->local_lock = listening_socket_data->local_lock;
+	socket_data->local_sockets = listening_socket_data->local_sockets;
+	socket_data->listening_socket_id = listening_socket->socket_id;
+	socket_data->next_incoming = ntohl(header->sequence_number);
+	socket_data->treshold = socket_data->next_incoming +
+	    ntohs(header->window);
+	socket_data->addrlen = addrlen;
+	socket_data->addr = malloc(socket_data->addrlen);
+	if (!socket_data->addr) {
+		free(socket_data);
+		return tcp_release_and_return(packet, ENOMEM);
+	}
+	memcpy(socket_data->addr, src, socket_data->addrlen);
+	socket_data->dest_port = ntohs(header->source_port);
+	if (ERROR_OCCURRED(tl_set_address_port(socket_data->addr,
+	    socket_data->addrlen, socket_data->dest_port))) {
+		free(socket_data->addr);
+		free(socket_data);
+		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+		return ERROR_CODE;
+	}
+
+	// create a socket
+	socket_id = -1;
+	if (ERROR_OCCURRED(socket_create(socket_data->local_sockets,
+	    listening_socket->phone, socket_data, &socket_id))) {
+		free(socket_data->addr);
+		free(socket_data);
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+
+	printf("new_sock %d\n", socket_id);
+	socket_data->pseudo_header = listening_socket_data->pseudo_header;
+	socket_data->headerlen = listening_socket_data->headerlen;
+	listening_socket_data->pseudo_header = NULL;
+	listening_socket_data->headerlen = 0;
+
+	fibril_rwlock_write_unlock(socket_data->local_lock);
+	fibril_rwlock_write_lock(&tcp_globals.lock);
+
+	// find the destination socket
+	listening_socket = socket_port_find(&tcp_globals.sockets,
+	    listening_port, SOCKET_MAP_KEY_LISTENING, 0);
+	if ((!listening_socket) ||
+	    (listening_socket->socket_id != listening_socket_id)) {
+		fibril_rwlock_write_unlock(&tcp_globals.lock);
+		// a shadow may remain until app hangs up
+		return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
+	}
+	listening_socket_data =
+	    (tcp_socket_data_ref) listening_socket->specific_data;
+	assert(listening_socket_data);
+
+	fibril_rwlock_write_lock(listening_socket_data->local_lock);
+
+	socket = socket_cores_find(listening_socket_data->local_sockets,
+	    socket_id);
+	if (!socket) {
+		// where is the socket?!?
+		fibril_rwlock_write_unlock(&tcp_globals.lock);
+		return ENOTSOCK;
+	}
+	socket_data = (tcp_socket_data_ref) socket->specific_data;
+	assert(socket_data);
+
+	ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port,
+	    socket, (const char *) socket_data->addr, socket_data->addrlen);
+	assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
+	    (const char *) socket_data->addr, socket_data->addrlen));
+
+//	ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket,
+//	    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+//	    tcp_globals.last_used_port);
+//	tcp_globals.last_used_port = socket->port;
+	fibril_rwlock_write_unlock(&tcp_globals.lock);
+	if (ERROR_CODE != EOK) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+
+	socket_data->state = TCP_SOCKET_LISTEN;
+	socket_data->next_incoming = ntohl(header->sequence_number) + 1;
+
+	// release additional packets
+	next_packet = pq_detach(packet);
+	if (next_packet) {
+		pq_release_remote(tcp_globals.net_phone,
+		    packet_get_id(next_packet));
+	}
+
+	// trim if longer than the header
+	if ((packet_get_data_length(packet) > sizeof(*header)) &&
+	    ERROR_OCCURRED(packet_trim(packet, 0,
+	    packet_get_data_length(packet) - sizeof(*header)))) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return tcp_release_and_return(packet, ERROR_CODE);
+	}
+
+	tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
+
+	if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return ERROR_CODE;
+	}
+
+	packet = tcp_get_packets_to_send(socket, socket_data);
+	if (!packet) {
+		socket_destroy(tcp_globals.net_phone, socket->socket_id,
+		    socket_data->local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data);
+		return EINVAL;
+	}
+
+	socket_data->state = TCP_SOCKET_SYN_RECEIVED;
+	fibril_rwlock_write_unlock(socket_data->local_lock);
+
+	// send the packet
+	tcp_send_packets(socket_data->device_id, packet);
+
+	return EOK;
+}
+
+int
+tcp_process_syn_received(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet)
+{
 	ERROR_DECLARE;
 
@@ -892,49 +1061,60 @@
 	assert(packet);
 
-	printf("syn_rec\n");
-	if(header->acknowledge){
-		// process acknowledgement
-		tcp_process_acknowledgement(socket, socket_data, header);
-
-		socket_data->next_incoming = ntohl(header->sequence_number);// + 1;
-		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-		socket_data->state = TCP_SOCKET_ESTABLISHED;
-		listening_socket = socket_cores_find(socket_data->local_sockets, socket_data->listening_socket_id);
-		if(listening_socket){
-			listening_socket_data = (tcp_socket_data_ref) listening_socket->specific_data;
-			assert(listening_socket_data);
-
-			// queue the received packet
-			if(! ERROR_OCCURRED(dyn_fifo_push(&listening_socket->accepted, (-1 * socket->socket_id), listening_socket_data->backlog))){
-				// notify the destination socket
-				async_msg_5(socket->phone, NET_SOCKET_ACCEPTED, (ipcarg_t) listening_socket->socket_id, socket_data->data_fragment_size, TCP_HEADER_SIZE, 0, (ipcarg_t) socket->socket_id);
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				return EOK;
-			}
-		}
-		// send FIN
-		socket_data->state = TCP_SOCKET_FIN_WAIT_1;
-
-		// create the notification packet
-		ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1));
-
+	if (!header->acknowledge)
+		return tcp_release_and_return(packet, EINVAL);
+
+	// process acknowledgement
+	tcp_process_acknowledgement(socket, socket_data, header);
+
+	socket_data->next_incoming = ntohl(header->sequence_number);	// + 1;
+	pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
+	socket_data->state = TCP_SOCKET_ESTABLISHED;
+	listening_socket = socket_cores_find(socket_data->local_sockets,
+	    socket_data->listening_socket_id);
+	if (listening_socket) {
+		listening_socket_data =
+		    (tcp_socket_data_ref) listening_socket->specific_data;
+		assert(listening_socket_data);
+
+		// queue the received packet
+		if (ERROR_NONE(dyn_fifo_push(&listening_socket->accepted,
+		    (-1 * socket->socket_id),
+		    listening_socket_data->backlog))) {
+
+			// notify the destination socket
+			async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
+			    (ipcarg_t) listening_socket->socket_id,
+			    socket_data->data_fragment_size, TCP_HEADER_SIZE,
+			    0, (ipcarg_t) socket->socket_id);
+
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+			return EOK;
+		}
+	}
+	// send FIN
+	socket_data->state = TCP_SOCKET_FIN_WAIT_1;
+
+	// create the notification packet
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 0, 1));
+
+	// send the packet
+	ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
+
+	// flush packets
+	packet = tcp_get_packets_to_send(socket, socket_data);
+	fibril_rwlock_write_unlock(socket_data->local_lock);
+	if (packet) {
 		// send the packet
-		ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1));
-
-		// flush packets
-		packet = tcp_get_packets_to_send(socket, socket_data);
-		fibril_rwlock_write_unlock(socket_data->local_lock);
-		if(packet){
-			// send the packet
-			tcp_send_packets(socket_data->device_id, packet);
-		}
-		return EOK;
-	}else{
-		return tcp_release_and_return(packet, EINVAL);
-	}
-	return EINVAL;
-}
-
-void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header){
+		tcp_send_packets(socket_data->device_id, packet);
+	}
+
+	return EOK;
+}
+
+void
+tcp_process_acknowledgement(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header)
+{
 	size_t number;
 	size_t length;
@@ -949,65 +1129,78 @@
 	assert(header);
 
-	if(header->acknowledge){
-		number = ntohl(header->acknowledgement_number);
-		// if more data acknowledged
-		if(number != socket_data->expected){
-			old = socket_data->expected;
-			if(IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing, number)){
-				switch(socket_data->state){
-					case TCP_SOCKET_FIN_WAIT_1:
-						socket_data->state = TCP_SOCKET_FIN_WAIT_2;
-						break;
-					case TCP_SOCKET_LAST_ACK:
-					case TCP_SOCKET_CLOSING:
-						// fin acknowledged - release the socket in another fibril
-						tcp_prepare_timeout(tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
-						break;
-					default:
-						break;
-				}
+	if (!header->acknowledge)
+		return;
+
+	number = ntohl(header->acknowledgement_number);
+	// if more data acknowledged
+	if (number != socket_data->expected) {
+		old = socket_data->expected;
+		if (IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing,
+		    number)) {
+			switch (socket_data->state) {
+			case TCP_SOCKET_FIN_WAIT_1:
+				socket_data->state = TCP_SOCKET_FIN_WAIT_2;
+				break;
+			case TCP_SOCKET_LAST_ACK:
+			case TCP_SOCKET_CLOSING:
+				// fin acknowledged - release the socket in
+				// another fibril
+				tcp_prepare_timeout(tcp_release_after_timeout,
+				    socket, socket_data, 0,
+				    TCP_SOCKET_TIME_WAIT,
+				    NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
+				break;
+			default:
+				break;
 			}
-			// update the treshold if higher than set
-			if(number + ntohs(header->window) > socket_data->expected + socket_data->treshold){
-				socket_data->treshold = number + ntohs(header->window) - socket_data->expected;
-			}
-			// set new expected sequence number
-			socket_data->expected = number;
+		}
+		// update the treshold if higher than set
+		if (number + ntohs(header->window) >
+		    socket_data->expected + socket_data->treshold) {
+			socket_data->treshold = number + ntohs(header->window) -
+			    socket_data->expected;
+		}
+		// set new expected sequence number
+		socket_data->expected = number;
+		socket_data->expected_count = 1;
+		packet = socket_data->outgoing;
+		while (pq_get_order(packet, &number, &length) == EOK) {
+			if (IS_IN_INTERVAL_OVERFLOW((uint32_t) old,
+			    (uint32_t) (number + length),
+			    (uint32_t) socket_data->expected)) {
+				next = pq_detach(packet);
+				if (packet == socket_data->outgoing)
+					socket_data->outgoing = next;
+
+				// add to acknowledged or release
+				if (pq_add(&acknowledged, packet, 0, 0) != EOK)
+					pq_release_remote(tcp_globals.net_phone,
+					    packet_get_id(packet));
+				packet = next;
+			} else if (old < socket_data->expected)
+				break;
+		}
+		// release acknowledged
+		if (acknowledged) {
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(acknowledged));
+		}
+		return;
+		// if the same as the previous time
+	}
+	if (number == socket_data->expected) {
+		// increase the counter
+		++socket_data->expected_count;
+		if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
 			socket_data->expected_count = 1;
-			packet = socket_data->outgoing;
-			while(pq_get_order(packet, &number, &length) == EOK){
-				if(IS_IN_INTERVAL_OVERFLOW((uint32_t) old, (uint32_t)(number + length), (uint32_t) socket_data->expected)){
-					next = pq_detach(packet);
-					if(packet == socket_data->outgoing){
-						socket_data->outgoing = next;
-					}
-					// add to acknowledged or release
-					if(pq_add(&acknowledged, packet, 0, 0) != EOK){
-						pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
-					}
-					packet = next;
-				}else if(old < socket_data->expected){
-					break;
-				}
-			}
-			// release acknowledged
-			if(acknowledged){
-				pq_release_remote(tcp_globals.net_phone, packet_get_id(acknowledged));
-			}
-			return;
-		// if the same as the previous time
-		}else if(number == socket_data->expected){
-			// increase the counter
-			++ socket_data->expected_count;
-			if(socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT){
-				socket_data->expected_count = 1;
-				// TODO retransmit lock
-				//tcp_retransmit_packet(socket, socket_data, number);
-			}
-		}
-	}
-}
-
-int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
+			// TODO retransmit lock
+			//tcp_retransmit_packet(socket, socket_data, number);
+		}
+	}
+}
+
+int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call,
+    ipc_call_t * answer, int *answer_count)
+{
 	ERROR_DECLARE;
 
@@ -1019,19 +1212,24 @@
 
 	*answer_count = 0;
-	switch(IPC_GET_METHOD(*call)){
-		case NET_TL_RECEIVED:
-			//fibril_rwlock_read_lock(&tcp_globals.lock);
-			if(! ERROR_OCCURRED(packet_translate_remote(tcp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
-				ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP, IPC_GET_ERROR(call));
-			}
-			//fibril_rwlock_read_unlock(&tcp_globals.lock);
-			return ERROR_CODE;
-		case IPC_M_CONNECT_TO_ME:
-			return tcp_process_client_messages(callid, * call);
-	}
+	switch (IPC_GET_METHOD(*call)) {
+	case NET_TL_RECEIVED:
+		//fibril_rwlock_read_lock(&tcp_globals.lock);
+		if (ERROR_NONE(packet_translate_remote(tcp_globals.net_phone,
+		    &packet, IPC_GET_PACKET(call)))) {
+			ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call),
+			    packet, SERVICE_TCP, IPC_GET_ERROR(call));
+		}
+		//fibril_rwlock_read_unlock(&tcp_globals.lock);
+		return ERROR_CODE;
+
+	case IPC_M_CONNECT_TO_ME:
+		return tcp_process_client_messages(callid, *call);
+	}
+
 	return ENOTSUP;
 }
 
-void tcp_refresh_socket_data(tcp_socket_data_ref socket_data){
+void tcp_refresh_socket_data(tcp_socket_data_ref socket_data)
+{
 	assert(socket_data);
 
@@ -1048,5 +1246,6 @@
 }
 
-void tcp_initialize_socket_data(tcp_socket_data_ref socket_data){
+void tcp_initialize_socket_data(tcp_socket_data_ref socket_data)
+{
 	assert(socket_data);
 
@@ -1057,10 +1256,11 @@
 }
 
-int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call){
+int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
+{
 	int res;
 	bool keep_on_going = true;
 	socket_cores_t local_sockets;
 	int app_phone = IPC_GET_PHONE(&call);
-	struct sockaddr * addr;
+	struct sockaddr *addr;
 	int socket_id;
 	size_t addrlen;
@@ -1083,168 +1283,208 @@
 	fibril_rwlock_initialize(&lock);
 
-	while(keep_on_going){
+	while (keep_on_going) {
 
 		// answer the call
 		answer_call(callid, res, &answer, answer_count);
-
 		// refresh data
 		refresh_answer(&answer, &answer_count);
-
 		// get the next call
 		callid = async_get_call(&call);
 
 		// process the call
-		switch(IPC_GET_METHOD(call)){
-			case IPC_M_PHONE_HUNGUP:
-				keep_on_going = false;
-				res = EHANGUP;
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			keep_on_going = false;
+			res = EHANGUP;
+			break;
+
+		case NET_SOCKET:
+			socket_data =
+			    (tcp_socket_data_ref) malloc(sizeof(*socket_data));
+			if (!socket_data) {
+				res = ENOMEM;
 				break;
-			case NET_SOCKET:
-				socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data));
-				if(! socket_data){
-					res = ENOMEM;
-				}else{
-					tcp_initialize_socket_data(socket_data);
-					socket_data->local_lock = &lock;
-					socket_data->local_sockets = &local_sockets;
-					fibril_rwlock_write_lock(&lock);
-					socket_id = SOCKET_GET_SOCKET_ID(call);
-					res = socket_create(&local_sockets, app_phone, socket_data, &socket_id);
-					SOCKET_SET_SOCKET_ID(answer, socket_id);
-					fibril_rwlock_write_unlock(&lock);
-					if(res == EOK){
-						if (tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){
-							SOCKET_SET_DATA_FRAGMENT_SIZE(answer, ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size));
-						}
-//						SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
-						SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
-						answer_count = 3;
-					}else{
-						free(socket_data);
-					}
+			}
+			
+			tcp_initialize_socket_data(socket_data);
+			socket_data->local_lock = &lock;
+			socket_data->local_sockets = &local_sockets;
+			fibril_rwlock_write_lock(&lock);
+			socket_id = SOCKET_GET_SOCKET_ID(call);
+			res = socket_create(&local_sockets, app_phone,
+			    socket_data, &socket_id);
+			SOCKET_SET_SOCKET_ID(answer, socket_id);
+			fibril_rwlock_write_unlock(&lock);
+			if (res != EOK) {
+				free(socket_data);
+				break;
+			}
+			if (tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+			    &tcp_globals.dimensions, DEVICE_INVALID_ID,
+			    &packet_dimension) == EOK) {
+				SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
+				    ((packet_dimension->content <
+				    socket_data->data_fragment_size) ?
+				    packet_dimension->content :
+				    socket_data->data_fragment_size));
+			}
+//                      SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
+			SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
+			answer_count = 3;
+			break;
+
+		case NET_SOCKET_BIND:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
+				break;
+			fibril_rwlock_write_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = socket_bind(&local_sockets, &tcp_globals.sockets,
+			    SOCKET_GET_SOCKET_ID(call), addr, addrlen,
+			    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+			    tcp_globals.last_used_port);
+			if (res == EOK) {
+				socket = socket_cores_find(&local_sockets,
+				    SOCKET_GET_SOCKET_ID(call));
+				if (socket) {
+					socket_data = (tcp_socket_data_ref)
+					    socket->specific_data;
+					assert(socket_data);
+					socket_data->state = TCP_SOCKET_LISTEN;
 				}
+			}
+			fibril_rwlock_write_unlock(&lock);
+			fibril_rwlock_write_unlock(&tcp_globals.lock);
+			free(addr);
+			break;
+
+		case NET_SOCKET_LISTEN:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+//                      fibril_rwlock_write_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_listen_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call),
+			    SOCKET_GET_BACKLOG(call));
+			fibril_rwlock_write_unlock(&lock);
+//                      fibril_rwlock_write_unlock(&tcp_globals.lock);
+			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			break;
+
+		case NET_SOCKET_CONNECT:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
 				break;
-			case NET_SOCKET_BIND:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					fibril_rwlock_write_lock(&tcp_globals.lock);
-					fibril_rwlock_write_lock(&lock);
-					res = socket_bind(&local_sockets, &tcp_globals.sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port);
-					if(res == EOK){
-						socket = socket_cores_find(&local_sockets, SOCKET_GET_SOCKET_ID(call));
-						if(socket){
-							socket_data = (tcp_socket_data_ref) socket->specific_data;
-							assert(socket_data);
-							socket_data->state = TCP_SOCKET_LISTEN;
-						}
-					}
-					fibril_rwlock_write_unlock(&lock);
-					fibril_rwlock_write_unlock(&tcp_globals.lock);
-					free(addr);
-				}
-				break;
-			case NET_SOCKET_LISTEN:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-//				fibril_rwlock_write_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_listen_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_BACKLOG(call));
+			// the global lock may be released in the
+			// tcp_connect_message() function
+			fibril_rwlock_write_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_connect_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call), addr, addrlen);
+			if (res != EOK) {
 				fibril_rwlock_write_unlock(&lock);
-//				fibril_rwlock_write_unlock(&tcp_globals.lock);
-				fibril_rwlock_read_unlock(&tcp_globals.lock);
-				break;
-			case NET_SOCKET_CONNECT:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					// the global lock may be released in the tcp_connect_message() function
-					fibril_rwlock_write_lock(&tcp_globals.lock);
-					fibril_rwlock_write_lock(&lock);
-					res = tcp_connect_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen);
-					if(res != EOK){
-						fibril_rwlock_write_unlock(&lock);
-						fibril_rwlock_write_unlock(&tcp_globals.lock);
-						free(addr);
-					}
-				}
-				break;
-			case NET_SOCKET_ACCEPT:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_accept_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
-				SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+				fibril_rwlock_write_unlock(&tcp_globals.lock);
+				free(addr);
+			}
+			break;
+
+		case NET_SOCKET_ACCEPT:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_accept_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call),
+			    SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
+			SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+			fibril_rwlock_write_unlock(&lock);
+			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			if (res > 0) {
+				SOCKET_SET_SOCKET_ID(answer, res);
+				SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
+				answer_count = 3;
+			}
+			break;
+
+		case NET_SOCKET_SEND:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_send_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call),
+			    SOCKET_GET_DATA_FRAGMENTS(call), &size,
+			    SOCKET_GET_FLAGS(call));
+			SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+			if (res != EOK) {
 				fibril_rwlock_write_unlock(&lock);
 				fibril_rwlock_read_unlock(&tcp_globals.lock);
-				if(res > 0){
-					SOCKET_SET_SOCKET_ID(answer, res);
-					SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
-					answer_count = 3;
-				}
+			} else {
+				answer_count = 2;
+			}
+			break;
+
+		case NET_SOCKET_SENDTO:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
 				break;
-			case NET_SOCKET_SEND:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_send_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
-				SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
-				if(res != EOK){
-					fibril_rwlock_write_unlock(&lock);
-					fibril_rwlock_read_unlock(&tcp_globals.lock);
-				}else{
-					answer_count = 2;
-				}
-				break;
-			case NET_SOCKET_SENDTO:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					fibril_rwlock_read_lock(&tcp_globals.lock);
-					fibril_rwlock_write_lock(&lock);
-					res = tcp_send_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
-					SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
-					if(res != EOK){
-						fibril_rwlock_write_unlock(&lock);
-						fibril_rwlock_read_unlock(&tcp_globals.lock);
-					}else{
-						answer_count = 2;
-					}
-					free(addr);
-				}
-				break;
-			case NET_SOCKET_RECV:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), NULL);
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_send_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call),
+			    SOCKET_GET_DATA_FRAGMENTS(call), &size,
+			    SOCKET_GET_FLAGS(call));
+			SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+			if (res != EOK) {
 				fibril_rwlock_write_unlock(&lock);
 				fibril_rwlock_read_unlock(&tcp_globals.lock);
-				if(res > 0){
-					SOCKET_SET_READ_DATA_LENGTH(answer, res);
-					answer_count = 1;
-					res = EOK;
-				}
-				break;
-			case NET_SOCKET_RECVFROM:
-				fibril_rwlock_read_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen);
+			} else {
+				answer_count = 2;
+			}
+			free(addr);
+			break;
+
+		case NET_SOCKET_RECV:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_recvfrom_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
+			    NULL);
+			fibril_rwlock_write_unlock(&lock);
+			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			if (res > 0) {
+				SOCKET_SET_READ_DATA_LENGTH(answer, res);
+				answer_count = 1;
+				res = EOK;
+			}
+			break;
+
+		case NET_SOCKET_RECVFROM:
+			fibril_rwlock_read_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_recvfrom_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
+			    &addrlen);
+			fibril_rwlock_write_unlock(&lock);
+			fibril_rwlock_read_unlock(&tcp_globals.lock);
+			if (res > 0) {
+				SOCKET_SET_READ_DATA_LENGTH(answer, res);
+				SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
+				answer_count = 3;
+				res = EOK;
+			}
+			break;
+
+		case NET_SOCKET_CLOSE:
+			fibril_rwlock_write_lock(&tcp_globals.lock);
+			fibril_rwlock_write_lock(&lock);
+			res = tcp_close_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call));
+			if (res != EOK) {
 				fibril_rwlock_write_unlock(&lock);
-				fibril_rwlock_read_unlock(&tcp_globals.lock);
-				if(res > 0){
-					SOCKET_SET_READ_DATA_LENGTH(answer, res);
-					SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
-					answer_count = 3;
-					res = EOK;
-				}
-				break;
-			case NET_SOCKET_CLOSE:
-				fibril_rwlock_write_lock(&tcp_globals.lock);
-				fibril_rwlock_write_lock(&lock);
-				res = tcp_close_message(&local_sockets, SOCKET_GET_SOCKET_ID(call));
-				if(res != EOK){
-					fibril_rwlock_write_unlock(&lock);
-					fibril_rwlock_write_unlock(&tcp_globals.lock);
-				}
-				break;
-			case NET_SOCKET_GETSOCKOPT:
-			case NET_SOCKET_SETSOCKOPT:
-			default:
-				res = ENOTSUP;
-				break;
+				fibril_rwlock_write_unlock(&tcp_globals.lock);
+			}
+			break;
+
+		case NET_SOCKET_GETSOCKOPT:
+		case NET_SOCKET_SETSOCKOPT:
+		default:
+			res = ENOTSUP;
+			break;
 		}
 	}
@@ -1255,10 +1495,12 @@
 	printf("release\n");
 	// release all local sockets
-	socket_cores_release(tcp_globals.net_phone, &local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
+	socket_cores_release(tcp_globals.net_phone, &local_sockets,
+	    &tcp_globals.sockets, tcp_free_socket_data);
 
 	return EOK;
 }
 
-int tcp_timeout(void * data){
+int tcp_timeout(void *data)
+{
 	tcp_timeout_ref timeout = data;
 	int keep_write_lock = false;
@@ -1271,53 +1513,61 @@
 	async_usleep(timeout->timeout);
 	// lock the globals
-	if(timeout->globals_read_only){
+	if (timeout->globals_read_only) 
 		fibril_rwlock_read_lock(&tcp_globals.lock);
-	}else{
+	else 
 		fibril_rwlock_write_lock(&tcp_globals.lock);
-	}
+
 	// find the pending operation socket
-	socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length);
-	if(socket && (socket->socket_id == timeout->socket_id)){
-		socket_data = (tcp_socket_data_ref) socket->specific_data;
-		assert(socket_data);
-		if(socket_data->local_sockets == timeout->local_sockets){
-			fibril_rwlock_write_lock(socket_data->local_lock);
-			if(timeout->sequence_number){
-				// increase the timeout counter;
-				++ socket_data->timeout_count;
-				if(socket_data->timeout_count == TCP_MAX_TIMEOUTS){
-					// TODO release as connection lost
-					//tcp_refresh_socket_data(socket_data);
-					fibril_rwlock_write_unlock(socket_data->local_lock);
-				}else{
-					// retransmit
-//					tcp_retransmit_packet(socket, socket_data, timeout->sequence_number);
-					fibril_rwlock_write_unlock(socket_data->local_lock);
-				}
-			}else{
-				fibril_mutex_lock(&socket_data->operation.mutex);
-				// set the timeout operation result if state not changed
-				if(socket_data->state == timeout->state){
-					socket_data->operation.result = ETIMEOUT;
-					// notify the main fibril
-					fibril_condvar_signal(&socket_data->operation.condvar);
-					// keep the global write lock
-					keep_write_lock = true;
-				}else{
-					// operation is ok, do nothing
-					// unlocking from now on, so the unlock order does not matter...
-					fibril_rwlock_write_unlock(socket_data->local_lock);
-				}
-				fibril_mutex_unlock(&socket_data->operation.mutex);
-			}
-		}
-	}
+	socket = socket_port_find(&tcp_globals.sockets, timeout->port,
+	    timeout->key, timeout->key_length);
+	if (!(socket && (socket->socket_id == timeout->socket_id)))
+		goto out;
+	
+	socket_data = (tcp_socket_data_ref) socket->specific_data;
+	assert(socket_data);
+	if (socket_data->local_sockets != timeout->local_sockets)
+		goto out;
+	
+	fibril_rwlock_write_lock(socket_data->local_lock);
+	if (timeout->sequence_number) {
+		// increase the timeout counter;
+		++socket_data->timeout_count;
+		if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
+			// TODO release as connection lost
+			//tcp_refresh_socket_data(socket_data);
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+		} else {
+			// retransmit
+//                      tcp_retransmit_packet(socket,
+//			    socket_data, timeout->sequence_number);
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+		}
+	} else {
+		fibril_mutex_lock(&socket_data->operation.mutex);
+		// set the timeout operation result if state not
+		// changed
+		if (socket_data->state == timeout->state) {
+			socket_data->operation.result = ETIMEOUT;
+			// notify the main fibril
+			fibril_condvar_signal(&socket_data->operation.condvar);
+			// keep the global write lock
+			keep_write_lock = true;
+		} else {
+			// operation is ok, do nothing
+			// unlocking from now on, so the unlocki
+			// order does not matter...
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+		}
+		fibril_mutex_unlock(&socket_data->operation.mutex);
+	}
+
+out:
 	// unlock only if no socket
-	if(timeout->globals_read_only){
+	if (timeout->globals_read_only)
 		fibril_rwlock_read_unlock(&tcp_globals.lock);
-	}else if(! keep_write_lock){
+	else if (!keep_write_lock)
 		// release if not desired
 		fibril_rwlock_write_unlock(&tcp_globals.lock);
-	}
+	
 	// release the timeout structure
 	free(timeout);
@@ -1325,9 +1575,10 @@
 }
 
-int tcp_release_after_timeout(void * data){
+int tcp_release_after_timeout(void *data)
+{
 	tcp_timeout_ref timeout = data;
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
-	fibril_rwlock_t * local_lock;
+	fibril_rwlock_t *local_lock;
 
 	assert(timeout);
@@ -1338,12 +1589,15 @@
 	fibril_rwlock_write_lock(&tcp_globals.lock);
 	// find the pending operation socket
-	socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length);
-	if(socket && (socket->socket_id == timeout->socket_id)){
+	socket = socket_port_find(&tcp_globals.sockets, timeout->port,
+	    timeout->key, timeout->key_length);
+	if (socket && (socket->socket_id == timeout->socket_id)) {
 		socket_data = (tcp_socket_data_ref) socket->specific_data;
 		assert(socket_data);
-		if(socket_data->local_sockets == timeout->local_sockets){
+		if (socket_data->local_sockets == timeout->local_sockets) {
 			local_lock = socket_data->local_lock;
 			fibril_rwlock_write_lock(local_lock);
-			socket_destroy(tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, &tcp_globals.sockets, tcp_free_socket_data);
+			socket_destroy(tcp_globals.net_phone,
+			    timeout->socket_id, timeout->local_sockets,
+			    &tcp_globals.sockets, tcp_free_socket_data);
 			fibril_rwlock_write_unlock(local_lock);
 		}
@@ -1356,5 +1610,8 @@
 }
 
-void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number){
+void
+tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    size_t sequence_number)
+{
 	packet_t packet;
 	packet_t copy;
@@ -1368,18 +1625,20 @@
 	packet = pq_find(socket_data->outgoing, sequence_number);
 	printf("retransmit %d\n", packet_get_id(packet));
-	if(packet){
+	if (packet) {
 		pq_get_order(packet, NULL, &data_length);
-		copy = tcp_prepare_copy(socket, socket_data, packet, data_length, sequence_number);
+		copy = tcp_prepare_copy(socket, socket_data, packet,
+		    data_length, sequence_number);
 		fibril_rwlock_write_unlock(socket_data->local_lock);
-//		printf("r send %d\n", packet_get_id(packet));
-		if(copy){
+//              printf("r send %d\n", packet_get_id(packet));
+		if (copy) 
 			tcp_send_packets(socket_data->device_id, copy);
-		}
-	}else{
+	} else {
 		fibril_rwlock_write_unlock(socket_data->local_lock);
 	}
 }
 
-int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog){
+int
+tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog)
+{
 	socket_core_ref socket;
 	tcp_socket_data_ref socket_data;
@@ -1387,12 +1646,12 @@
 	assert(local_sockets);
 
-	if(backlog < 0){
+	if (backlog < 0) 
 		return EINVAL;
-	}
+
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket) 
 		return ENOTSOCK;
-	}
+	
 	// get the socket specific data
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
@@ -1400,8 +1659,12 @@
 	// set the backlog
 	socket_data->backlog = backlog;
+
 	return EOK;
 }
 
-int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen){
+int
+tcp_connect_message(socket_cores_ref local_sockets, int socket_id,
+    struct sockaddr *addr, socklen_t addrlen)
+{
 	ERROR_DECLARE;
 
@@ -1414,12 +1677,14 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
-	if(ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr, addrlen))){
+	
+	if (ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr,
+	    addrlen))) {
 		tcp_free_socket_data(socket);
 		// unbind if bound
-		if(socket->port > 0){
-			socket_ports_exclude(&tcp_globals.sockets, socket->port);
+		if (socket->port > 0) {
+			socket_ports_exclude(&tcp_globals.sockets,
+			    socket->port);
 			socket->port = 0;
 		}
@@ -1428,5 +1693,8 @@
 }
 
-int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen){
+int
+tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets,
+    struct sockaddr *addr, socklen_t addrlen)
+{
 	ERROR_DECLARE;
 
@@ -1442,20 +1710,28 @@
 	assert(socket_data);
 	assert(socket->specific_data == socket_data);
-	if((socket_data->state != TCP_SOCKET_INITIAL)
-		&& ((socket_data->state != TCP_SOCKET_LISTEN) || (socket->port <= 0))){
+	if ((socket_data->state != TCP_SOCKET_INITIAL) &&
+	    ((socket_data->state != TCP_SOCKET_LISTEN) ||
+	    (socket->port <= 0)))
 		return EINVAL;
-	}
+
 	// get the destination port
-	ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &socket_data->dest_port));
-	if(socket->port <= 0){
+	ERROR_PROPAGATE(tl_get_address_port(addr, addrlen,
+	    &socket_data->dest_port));
+	if (socket->port <= 0) {
 		// try to find a free port
-		ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port));
+		ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets,
+		    socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+		    tcp_globals.last_used_port));
 		// set the next port as the search starting port number
 		tcp_globals.last_used_port = socket->port;
 	}
-	ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP, addr, addrlen, &socket_data->device_id, &socket_data->pseudo_header, &socket_data->headerlen));
+
+	ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
+	    addr, addrlen, &socket_data->device_id,
+	    &socket_data->pseudo_header, &socket_data->headerlen));
 
 	// create the notification packet
-	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 1, 0));
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 1, 0));
 
 	// unlock the globals and wait for an operation
@@ -1465,12 +1741,16 @@
 	socket_data->addrlen = addrlen;
 	// send the packet
-	if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))
-		|| ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))){
+	if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1)) ||
+	    ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data,
+	    0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))) {
+
 		socket_data->addr = NULL;
 		socket_data->addrlen = 0;
 		fibril_rwlock_write_lock(&tcp_globals.lock);
-	}else{
+
+	} else {
+
 		packet = tcp_get_packets_to_send(socket, socket_data);
-		if(packet){
+		if (packet) {
 			fibril_mutex_lock(&socket_data->operation.mutex);
 			fibril_rwlock_write_unlock(socket_data->local_lock);
@@ -1478,12 +1758,14 @@
 			printf("connecting %d\n", packet_get_id(packet));
 			tcp_send_packets(socket_data->device_id, packet);
+
 			// wait for a reply
-			fibril_condvar_wait(&socket_data->operation.condvar, &socket_data->operation.mutex);
+			fibril_condvar_wait(&socket_data->operation.condvar,
+			    &socket_data->operation.mutex);
 			ERROR_CODE = socket_data->operation.result;
-			if(ERROR_CODE != EOK){
+			if (ERROR_CODE != EOK) {
 				socket_data->addr = NULL;
 				socket_data->addrlen = 0;
 			}
-		}else{
+		} else {
 			socket_data->addr = NULL;
 			socket_data->addrlen = 0;
@@ -1498,5 +1780,8 @@
 }
 
-int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){
+int
+tcp_queue_prepare_packet(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, packet_t packet, size_t data_length)
+{
 	ERROR_DECLARE;
 
@@ -1509,21 +1794,26 @@
 	// get tcp header
 	header = (tcp_header_ref) packet_get_data(packet);
-	if(! header){
+	if (!header)
 		return NO_DATA;
-	}
+	
 	header->destination_port = htons(socket_data->dest_port);
 	header->source_port = htons(socket->port);
 	header->sequence_number = htonl(socket_data->next_outgoing);
-	if(ERROR_OCCURRED(packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr, socket_data->addrlen))){
+
+	if (ERROR_OCCURRED(packet_set_addr(packet, NULL,
+	    (uint8_t *) socket_data->addr, socket_data->addrlen)))
 		return tcp_release_and_return(packet, EINVAL);
-	}
+
 	// remember the outgoing FIN
-	if(header->finalize){
+	if (header->finalize) 
 		socket_data->fin_outgoing = socket_data->next_outgoing;
-	}
+	
 	return EOK;
 }
 
-int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){
+int
+tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    packet_t packet, size_t data_length)
+{
 	ERROR_DECLARE;
 
@@ -1532,14 +1822,18 @@
 	assert(socket->specific_data == socket_data);
 
-	ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, data_length));
-
-	if(ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing, data_length))){
+	ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet,
+	    data_length));
+
+	if (ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet,
+	    socket_data->next_outgoing, data_length)))
 		return tcp_release_and_return(packet, ERROR_CODE);
-	}
+
 	socket_data->next_outgoing += data_length;
 	return EOK;
 }
 
-packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data){
+packet_t
+tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data)
+{
 	ERROR_DECLARE;
 
@@ -1555,38 +1849,47 @@
 
 	packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1);
-	while(packet){
+	while (packet) {
 		pq_get_order(packet, NULL, &data_length);
+
 		// send only if fits into the window
 		// respecting the possible overflow
-		if(IS_IN_INTERVAL_OVERFLOW((uint32_t) socket_data->last_outgoing, (uint32_t)(socket_data->last_outgoing + data_length), (uint32_t)(socket_data->expected + socket_data->treshold))){
-			copy = tcp_prepare_copy(socket, socket_data, packet, data_length, socket_data->last_outgoing + 1);
-			if(! copy){
-				return sending;
-			}
-			if(! sending){
-				sending = copy;
-			}else{
-				if(ERROR_OCCURRED(pq_insert_after(previous, copy))){
-					pq_release_remote(tcp_globals.net_phone, packet_get_id(copy));
-					return sending;
-				}
-			}
-			previous = copy;
-			packet = pq_next(packet);
-			// overflow occurred ?
-			if((! packet) && (socket_data->last_outgoing > socket_data->next_outgoing)){
-				printf("gpts overflow\n");
-				// continue from the beginning
-				packet = socket_data->outgoing;
-			}
-			socket_data->last_outgoing += data_length;
-		}else{
+		if (!IS_IN_INTERVAL_OVERFLOW(
+		    (uint32_t) socket_data->last_outgoing,
+		    (uint32_t) (socket_data->last_outgoing + data_length),
+		    (uint32_t) (socket_data->expected + socket_data->treshold)))
 			break;
-		}
-	}
+
+		copy = tcp_prepare_copy(socket, socket_data, packet,
+		    data_length, socket_data->last_outgoing + 1);
+		if (!copy) 
+			return sending;
+			
+		if (!sending) {
+			sending = copy;
+		} else if (ERROR_OCCURRED(pq_insert_after(previous, copy))) {
+			pq_release_remote(tcp_globals.net_phone,
+			    packet_get_id(copy));
+			return sending;
+		}
+
+		previous = copy;
+		packet = pq_next(packet);
+		// overflow occurred ?
+		if ((!packet) &&
+		    (socket_data->last_outgoing > socket_data->next_outgoing)) {
+			printf("gpts overflow\n");
+			// continue from the beginning
+			packet = socket_data->outgoing;
+		}
+		socket_data->last_outgoing += data_length;
+	}
+
 	return sending;
 }
 
-packet_t tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number){
+packet_t
+tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    packet_t packet, size_t data_length, size_t sequence_number)
+{
 	ERROR_DECLARE;
 
@@ -1599,5 +1902,7 @@
 
 	// adjust the pseudo header
-	if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length(packet)))){
+	if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
+	    socket_data->pseudo_header, socket_data->headerlen,
+	    packet_get_data_length(packet)))) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
@@ -1606,5 +1911,5 @@
 	// get the header
 	header = (tcp_header_ref) packet_get_data(packet);
-	if(! header){
+	if (!header) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
@@ -1613,6 +1918,7 @@
 
 	// adjust the header
-	if(socket_data->next_incoming){
-		header->acknowledgement_number = htonl(socket_data->next_incoming);
+	if (socket_data->next_incoming) {
+		header->acknowledgement_number =
+		    htonl(socket_data->next_incoming);
 		header->acknowledge = 1;
 	}
@@ -1621,18 +1927,26 @@
 	// checksum
 	header->checksum = 0;
-	checksum = compute_checksum(0, socket_data->pseudo_header, socket_data->headerlen);
-	checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet), packet_get_data_length(packet));
+	checksum = compute_checksum(0, socket_data->pseudo_header,
+	    socket_data->headerlen);
+	checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet),
+	    packet_get_data_length(packet));
 	header->checksum = htons(flip_checksum(compact_checksum(checksum)));
+
 	// prepare the packet
-	if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0))
-	// prepare the timeout
-		|| ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true))){
+	if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0,
+	    0, 0)) || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket,
+	    socket_data, sequence_number, socket_data->state,
+	    socket_data->timeout, true))) {
 		pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 		return NULL;
 	}
+
 	return packet;
 }
 
-packet_t tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number){
+packet_t
+tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data,
+    packet_t packet, size_t data_length, size_t sequence_number)
+{
 	packet_t copy;
 
@@ -1643,22 +1957,28 @@
 	// make a copy of the packet
 	copy = packet_get_copy(tcp_globals.net_phone, packet);
-	if(! copy){
+	if (!copy)
 		return NULL;
-	}
-
-	return tcp_send_prepare_packet(socket, socket_data, copy, data_length, sequence_number);
-}
-
-void tcp_send_packets(device_id_t device_id, packet_t packet){
+
+	return tcp_send_prepare_packet(socket, socket_data, copy, data_length,
+	    sequence_number);
+}
+
+void tcp_send_packets(device_id_t device_id, packet_t packet)
+{
 	packet_t next;
 
-	while(packet){
+	while (packet) {
 		next = pq_detach(packet);
-		ip_send_msg(tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0);
+		ip_send_msg(tcp_globals.ip_phone, device_id, packet,
+		    SERVICE_TCP, 0);
 		packet = next;
 	}
 }
 
-void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize){
+void
+tcp_prepare_operation_header(socket_core_ref socket,
+    tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize,
+    int finalize)
+{
 	assert(socket);
 	assert(socket_data);
@@ -1674,5 +1994,10 @@
 }
 
-int tcp_prepare_timeout(int (*timeout_function)(void * tcp_timeout_t), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only){
+int
+tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
+    socket_core_ref socket, tcp_socket_data_ref socket_data,
+    size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
+    int globals_read_only)
+{
 	tcp_timeout_ref operation_timeout;
 	fid_t fibril;
@@ -1683,8 +2008,9 @@
 
 	// prepare the timeout with key bundle structure
-	operation_timeout = malloc(sizeof(*operation_timeout) + socket->key_length + 1);
-	if(! operation_timeout){
+	operation_timeout = malloc(sizeof(*operation_timeout) +
+	    socket->key_length + 1);
+	if (!operation_timeout)
 		return ENOMEM;
-	}
+
 	bzero(operation_timeout, sizeof(*operation_timeout));
 	operation_timeout->globals_read_only = globals_read_only;
@@ -1697,5 +2023,6 @@
 
 	// copy the key
-	operation_timeout->key = ((char *) operation_timeout) + sizeof(*operation_timeout);
+	operation_timeout->key = ((char *) operation_timeout) +
+	    sizeof(*operation_timeout);
 	operation_timeout->key_length = socket->key_length;
 	memcpy(operation_timeout->key, socket->key, socket->key_length);
@@ -1704,9 +2031,9 @@
 	// prepare the timeouting thread
 	fibril = fibril_create(timeout_function, operation_timeout);
-	if(! fibril){
+	if (!fibril) {
 		free(operation_timeout);
 		return EPARTY;
 	}
-//	fibril_mutex_lock(&socket_data->operation.mutex);
+//      fibril_mutex_lock(&socket_data->operation.mutex);
 	// start the timeouting fibril
 	fibril_add_ready(fibril);
@@ -1715,5 +2042,8 @@
 }
 
-int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){
+int
+tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
+    size_t * addrlen)
+{
 	ERROR_DECLARE;
 
@@ -1728,21 +2058,22 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the socket specific data
-	if(! socket->specific_data){
+	if (!socket->specific_data)
 		return NO_DATA;
-	}
+
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
 
 	// check state
-	if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){
+	if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
+	    (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
 		return ENOTCONN;
-	}
 
 	// send the source address if desired
-	if(addrlen){
-		ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen));
+	if (addrlen) {
+		ERROR_PROPAGATE(data_reply(socket_data->addr,
+		    socket_data->addrlen));
 		*addrlen = socket_data->addrlen;
 	}
@@ -1750,8 +2081,9 @@
 	// get the next received packet
 	packet_id = dyn_fifo_value(&socket->received);
-	if(packet_id < 0){
+	if (packet_id < 0)
 		return NO_DATA;
-	}
-	ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet, packet_id));
+
+	ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet,
+	    packet_id));
 
 	// reply the packets
@@ -1765,5 +2097,8 @@
 }
 
-int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags){
+int
+tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments,
+    size_t * data_fragment_size, int flags)
+{
 	ERROR_DECLARE;
 
@@ -1782,36 +2117,42 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the socket specific data
-	if(! socket->specific_data){
+	if (!socket->specific_data)
 		return NO_DATA;
-	}
+
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
 
 	// check state
-	if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){
+	if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
+	    (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
 		return ENOTCONN;
-	}
-
-	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
-
-	*data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
-
-	for(index = 0; index < fragments; ++ index){
+
+	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	   &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
+
+	*data_fragment_size =
+	    ((packet_dimension->content < socket_data->data_fragment_size) ?
+	    packet_dimension->content : socket_data->data_fragment_size);
+
+	for (index = 0; index < fragments; ++index) {
 		// read the data fragment
-		result = tl_socket_read_packet_data(tcp_globals.net_phone, &packet, TCP_HEADER_SIZE, packet_dimension, socket_data->addr, socket_data->addrlen);
-		if(result < 0){
+		result = tl_socket_read_packet_data(tcp_globals.net_phone,
+		    &packet, TCP_HEADER_SIZE, packet_dimension,
+		    socket_data->addr, socket_data->addrlen);
+		if (result < 0)
 			return result;
-		}
+
 		total_length = (size_t) result;
 		// prefix the tcp header
 		header = PACKET_PREFIX(packet, tcp_header_t);
-		if(! header){
+		if (!header)
 			return tcp_release_and_return(packet, ENOMEM);
-		}
+
 		tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
-		ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 0));
+		ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet,
+		    0));
 	}
 
@@ -1820,5 +2161,6 @@
 	fibril_rwlock_write_unlock(socket_data->local_lock);
 	fibril_rwlock_read_unlock(&tcp_globals.lock);
-	if(packet){
+
+	if (packet) {
 		// send the packet
 		tcp_send_packets(socket_data->device_id, packet);
@@ -1828,5 +2170,7 @@
 }
 
-int tcp_close_message(socket_cores_ref local_sockets, int socket_id){
+int
+tcp_close_message(socket_cores_ref local_sockets, int socket_id)
+{
 	ERROR_DECLARE;
 
@@ -1837,7 +2181,7 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the socket specific data
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
@@ -1845,25 +2189,32 @@
 
 	// check state
-	switch(socket_data->state){
-		case TCP_SOCKET_ESTABLISHED:
-			socket_data->state = TCP_SOCKET_FIN_WAIT_1;
-			break;
-		case TCP_SOCKET_CLOSE_WAIT:
-			socket_data->state = TCP_SOCKET_LAST_ACK;
-			break;
-//		case TCP_SOCKET_LISTEN:
-		default:
-			// just destroy
-			if(! ERROR_OCCURRED(socket_destroy(tcp_globals.net_phone, socket_id, local_sockets, &tcp_globals.sockets, tcp_free_socket_data))){
-				fibril_rwlock_write_unlock(socket_data->local_lock);
-				fibril_rwlock_write_unlock(&tcp_globals.lock);
-			}
-			return ERROR_CODE;
-	}
+	switch (socket_data->state) {
+	case TCP_SOCKET_ESTABLISHED:
+		socket_data->state = TCP_SOCKET_FIN_WAIT_1;
+		break;
+
+	case TCP_SOCKET_CLOSE_WAIT:
+		socket_data->state = TCP_SOCKET_LAST_ACK;
+		break;
+
+//      case TCP_SOCKET_LISTEN:
+
+	default:
+		// just destroy
+		if (ERROR_NONE(socket_destroy(tcp_globals.net_phone, socket_id,
+		    local_sockets, &tcp_globals.sockets,
+		    tcp_free_socket_data))) {
+			fibril_rwlock_write_unlock(socket_data->local_lock);
+			fibril_rwlock_write_unlock(&tcp_globals.lock);
+		}
+		return ERROR_CODE;
+	}
+
 	// send FIN
 	// TODO should I wait to complete?
 
 	// create the notification packet
-	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1));
+	ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket,
+	    socket_data, 0, 1));
 
 	// send the packet
@@ -1874,12 +2225,17 @@
 	fibril_rwlock_write_unlock(socket_data->local_lock);
 	fibril_rwlock_write_unlock(&tcp_globals.lock);
-	if(packet){
+
+	if (packet) {
 		// send the packet
 		tcp_send_packets(socket_data->device_id, packet);
 	}
+
 	return EOK;
 }
 
-int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize){
+int
+tcp_create_notification_packet(packet_t * packet, socket_core_ref socket,
+    tcp_socket_data_ref socket_data, int synchronize, int finalize)
+{
 	ERROR_DECLARE;
 
@@ -1890,21 +2246,31 @@
 
 	// get the device packet dimension
-	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
+	ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone,
+	    &tcp_globals.dimensions, socket_data->device_id,
+	    &packet_dimension));
+
 	// get a new packet
-	*packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE, packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix);
-	if(! * packet){
+	*packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE,
+	    packet_dimension->addr_len, packet_dimension->prefix,
+	    packet_dimension->suffix);
+	
+	if (!*packet) 
 		return ENOMEM;
-	}
+
 	// allocate space in the packet
 	header = PACKET_SUFFIX(*packet, tcp_header_t);
-	if(! header){
+	if (!header)
 		tcp_release_and_return(*packet, ENOMEM);
-	}
-
-	tcp_prepare_operation_header(socket, socket_data, header, synchronize, finalize);
+
+	tcp_prepare_operation_header(socket, socket_data, header, synchronize,
+	    finalize);
+
 	return EOK;
 }
 
-int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen){
+int
+tcp_accept_message(socket_cores_ref local_sockets, int socket_id,
+    int new_socket_id, size_t * data_fragment_size, size_t * addrlen)
+{
 	ERROR_DECLARE;
 
@@ -1920,7 +2286,7 @@
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the socket specific data
 	socket_data = (tcp_socket_data_ref) socket->specific_data;
@@ -1928,40 +2294,52 @@
 
 	// check state
-	if(socket_data->state != TCP_SOCKET_LISTEN){
+	if (socket_data->state != TCP_SOCKET_LISTEN)
 		return EINVAL;
-	}
-
-	do{
+
+	do {
 		socket_id = dyn_fifo_value(&socket->accepted);
-		if(socket_id < 0){
+		if (socket_id < 0)
 			return ENOTSOCK;
-		}
 		socket_id *= -1;
 
 		accepted = socket_cores_find(local_sockets, socket_id);
-		if(! accepted){
+		if (!accepted)
 			return ENOTSOCK;
-		}
+
 		// get the socket specific data
 		socket_data = (tcp_socket_data_ref) accepted->specific_data;
 		assert(socket_data);
 		// TODO can it be in another state?
-		if(socket_data->state == TCP_SOCKET_ESTABLISHED){
-			ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen));
-			ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension));
+		if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
+			ERROR_PROPAGATE(data_reply(socket_data->addr,
+			    socket_data->addrlen));
+			ERROR_PROPAGATE(tl_get_ip_packet_dimension(
+			    tcp_globals.ip_phone, &tcp_globals.dimensions,
+			    socket_data->device_id, &packet_dimension));
 			*addrlen = socket_data->addrlen;
-			*data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size);
-			if(new_socket_id > 0){
-				ERROR_PROPAGATE(socket_cores_update(local_sockets, accepted->socket_id, new_socket_id));
+
+			*data_fragment_size =
+			    ((packet_dimension->content <
+			    socket_data->data_fragment_size) ?
+			    packet_dimension->content :
+			    socket_data->data_fragment_size);
+	
+			if (new_socket_id > 0) {
+				ERROR_PROPAGATE(socket_cores_update(
+				    local_sockets, accepted->socket_id,
+				    new_socket_id));
 				accepted->socket_id = new_socket_id;
 			}
 		}
 		dyn_fifo_pop(&socket->accepted);
-	}while(socket_data->state != TCP_SOCKET_ESTABLISHED);
+	} while (socket_data->state != TCP_SOCKET_ESTABLISHED);
+
 	printf("ret accept %d\n", accepted->socket_id);
 	return accepted->socket_id;
 }
 
-void tcp_free_socket_data(socket_core_ref socket){
+void
+tcp_free_socket_data(socket_core_ref socket)
+{
 	tcp_socket_data_ref socket_data;
 
@@ -1974,6 +2352,6 @@
 	assert(socket_data);
 	//free the pseudo header
-	if(socket_data->pseudo_header){
-		if(socket_data->headerlen){
+	if (socket_data->pseudo_header) {
+		if (socket_data->headerlen) {
 			printf("d pseudo\n");
 			free(socket_data->pseudo_header);
@@ -1984,6 +2362,6 @@
 	socket_data->headerlen = 0;
 	// free the address
-	if(socket_data->addr){
-		if(socket_data->addrlen){
+	if (socket_data->addr) {
+		if (socket_data->addrlen) {
 			printf("d addr\n");
 			free(socket_data->addr);
@@ -1995,5 +2373,6 @@
 }
 
-int tcp_release_and_return(packet_t packet, int result){
+int tcp_release_and_return(packet_t packet, int result)
+{
 	pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
 	return result;
@@ -2013,25 +2392,36 @@
 	 */
 	ipc_answer_0(iid, EOK);
-	
-	while(true) {
+
+	while (true) {
 		ipc_call_t answer;
 		int answer_count;
-		
-		/* Clear the answer structure */
+
+		/*
+		   Clear the answer structure 
+		 */
 		refresh_answer(&answer, &answer_count);
-		
-		/* Fetch the next message */
+
+		/*
+		   Fetch the next message 
+		 */
 		ipc_call_t call;
 		ipc_callid_t callid = async_get_call(&call);
-		
-		/* Process the message */
+
+		/*
+		   Process the message 
+		 */
 		int res = tl_module_message_standalone(callid, &call, &answer,
 		    &answer_count);
-		
-		/* End if said to either by the message or the processing result */
-		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
+
+		/*
+		   End if said to either by the message or the processing result 
+		 */
+		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
+		    (res == EHANGUP))
 			return;
-		
-		/* Answer the message */
+
+		/*
+		   Answer the message 
+		 */
 		answer_call(callid, res, &answer, answer_count);
 	}
@@ -2047,12 +2437,15 @@
  *
  */
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
 {
 	ERROR_DECLARE;
-	
-	/* Start the module */
+
+	/*
+	   Start the module 
+	 */
 	if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection)))
 		return ERROR_CODE;
-	
+
 	return EOK;
 }
Index: uspace/srv/net/tl/tcp/tcp_module.c
===================================================================
--- uspace/srv/net/tl/tcp/tcp_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/tcp/tcp_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,8 +40,8 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <packet/packet.h>
Index: uspace/srv/net/tl/udp/udp.c
===================================================================
--- uspace/srv/net/tl/udp/udp.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/udp/udp.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -42,6 +42,7 @@
 #include <ipc/ipc.h>
 #include <ipc/services.h>
-
-#include <net_err.h>
+#include <errno.h>
+#include <err.h>
+
 #include <net_messages.h>
 #include <net_modules.h>
@@ -60,5 +61,4 @@
 #include <net_interface.h>
 #include <socket_codes.h>
-#include <socket_errno.h>
 #include <socket_core.h>
 #include <socket_messages.h>
@@ -72,61 +72,69 @@
 #include "udp_module.h"
 
-/** UDP module name.
- */
+/** UDP module name. */
 #define NAME	"UDP protocol"
 
-/** Default UDP checksum computing.
- */
+/** Default UDP checksum computing. */
 #define NET_DEFAULT_UDP_CHECKSUM_COMPUTING	true
 
-/** Default UDP autobind when sending via unbound sockets.
- */
+/** Default UDP autobind when sending via unbound sockets. */
 #define NET_DEFAULT_UDP_AUTOBINDING	true
 
-/** Maximum UDP fragment size.
- */
-#define MAX_UDP_FRAGMENT_SIZE	65535
-
-/** Free ports pool start.
- */
-#define UDP_FREE_PORTS_START	1025
-
-/** Free ports pool end.
- */
+/** Maximum UDP fragment size. */
+#define MAX_UDP_FRAGMENT_SIZE		65535
+
+/** Free ports pool start. */
+#define UDP_FREE_PORTS_START		1025
+
+/** Free ports pool end. */
 #define UDP_FREE_PORTS_END		65535
 
 /** Processes the received UDP packet queue.
+ *
  *  Is used as an entry point from the underlying IP module.
  *  Locks the global lock and calls udp_process_packet() function.
+ *
  *  @param[in] device_id The receiving device identifier.
  *  @param[in,out] packet The received packet queue.
- *  @param receiver The target service. Ignored parameter.
- *  @param[in] error The packet error reporting service. Prefixes the received packet.
- *  @returns EOK on success.
- *  @returns Other error codes as defined for the udp_process_packet() function.
- */
-int udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error);
+ *  @param receiver	The target service. Ignored parameter.
+ *  @param[in] error	The packet error reporting service. Prefixes the
+ *			received packet.
+ *  @returns		EOK on success.
+ *  @returns		Other error codes as defined for the
+ *			udp_process_packet() function.
+ */
+int
+udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
+    services_t error);
 
 /** Processes the received UDP packet queue.
+ *
  *  Notifies the destination socket application.
- *  Releases the packet on error or sends an ICMP error notification..
+ *  Releases the packet on error or sends an ICMP error notification.
+ *
  *  @param[in] device_id The receiving device identifier.
  *  @param[in,out] packet The received packet queue.
- *  @param[in] error The packet error reporting service. Prefixes the received packet.
- *  @returns EOK on success.
- *  @returns EINVAL if the packet is not valid.
- *  @returns EINVAL if the stored packet address is not the an_addr_t.
- *  @returns EINVAL if the packet does not contain any data.
- *  @returns NO_DATA if the packet content is shorter than the user datagram header.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns EADDRNOTAVAIL if the destination socket does not exist.
- *  @returns Other error codes as defined for the ip_client_process_packet() function.
- */
-int udp_process_packet(device_id_t device_id, packet_t packet, services_t error);
+ *  @param[in] error	The packet error reporting service. Prefixes the
+ *			received packet.
+ *  @returns		EOK on success.
+ *  @returns		EINVAL if the packet is not valid.
+ *  @returns		EINVAL if the stored packet address is not the
+ *			an_addr_t.
+ *  @returns		EINVAL if the packet does not contain any data.
+ *  @returns 		NO_DATA if the packet content is shorter than the user
+ *			datagram header.
+ *  @returns		ENOMEM if there is not enough memory left.
+ *  @returns		EADDRNOTAVAIL if the destination socket does not exist.
+ *  @returns		Other error codes as defined for the
+ *			ip_client_process_packet() function.
+ */
+int
+udp_process_packet(device_id_t device_id, packet_t packet, services_t error);
 
 /** Releases the packet and returns the result.
- *  @param[in] packet The packet queue to be released.
- *  @param[in] result The result to be returned.
- *  @return The result parameter.
+ *
+ *  @param[in] packet	The packet queue to be released.
+ *  @param[in] result	The result to be returned.
+ *  @return		The result parameter.
  */
 int udp_release_and_return(packet_t packet, int result);
@@ -137,51 +145,68 @@
 
 /** Processes the socket client messages.
+ *
  *  Runs until the client module disconnects.
- *  @param[in] callid The message identifier.
- *  @param[in] call The message parameters.
- *  @returns EOK on success.
- *  @see socket.h
+ *
+ *  @param[in] callid 	The message identifier.
+ *  @param[in] call	The message parameters.
+ *  @returns		EOK on success.
+ *  @see		socket.h
  */
 int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
 
 /** Sends data from the socket to the remote address.
+ *
  *  Binds the socket to a free port if not already connected/bound.
  *  Handles the NET_SOCKET_SENDTO message.
  *  Supports AF_INET and AF_INET6 address families.
+ *
  *  @param[in,out] local_sockets The application local sockets.
  *  @param[in] socket_id Socket identifier.
- *  @param[in] addr The destination address.
- *  @param[in] addrlen The address length.
+ *  @param[in] addr	The destination address.
+ *  @param[in] addrlen	The address length.
  *  @param[in] fragments The number of data fragments.
  *  @param[out] data_fragment_size The data fragment size in bytes.
- *  @param[in] flags Various send flags.
- *  @returns EOK on success.
- *  @returns EAFNOTSUPPORT if the address family is not supported.
- *  @returns ENOTSOCK if the socket is not found.
- *  @returns EINVAL if the address is invalid.
- *  @returns ENOTCONN if the sending socket is not and cannot be bound.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns Other error codes as defined for the socket_read_packet_data() function.
- *  @returns Other error codes as defined for the ip_client_prepare_packet() function.
- *  @returns Other error codes as defined for the ip_send_msg() function.
- */
-int udp_sendto_message(socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags);
+ *  @param[in] flags	Various send flags.
+ *  @returns		EOK on success.
+ *  @returns		EAFNOTSUPPORT if the address family is not supported.
+ *  @returns		ENOTSOCK if the socket is not found.
+ *  @returns		EINVAL if the address is invalid.
+ *  @returns		ENOTCONN if the sending socket is not and cannot be
+ *			bound.
+ *  @returns		ENOMEM if there is not enough memory left.
+ *  @returns		Other error codes as defined for the
+ *			socket_read_packet_data() function.
+ *  @returns		Other error codes as defined for the
+ *			ip_client_prepare_packet() function.
+ *  @returns		Other error codes as defined for the ip_send_msg()
+ *			function.
+ */
+int
+udp_sendto_message(socket_cores_ref local_sockets, int socket_id,
+    const struct sockaddr * addr, socklen_t addrlen, int fragments,
+    size_t * data_fragment_size, int flags);
 
 /** Receives data to the socket.
+ *
  *  Handles the NET_SOCKET_RECVFROM message.
  *  Replies the source address as well.
+ *
  *  @param[in] local_sockets The application local sockets.
  *  @param[in] socket_id Socket identifier.
- *  @param[in] flags Various receive flags.
- *  @param[out] addrlen The source address length.
- *  @returns The number of bytes received.
- *  @returns ENOTSOCK if the socket is not found.
- *  @returns NO_DATA if there are no received packets or data.
- *  @returns ENOMEM if there is not enough memory left.
- *  @returns EINVAL if the received address is not an IP address.
- *  @returns Other error codes as defined for the packet_translate() function.
- *  @returns Other error codes as defined for the data_reply() function.
- */
-int udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen);
+ *  @param[in] flags	Various receive flags.
+ *  @param[out] addrlen	The source address length.
+ *  @returns		The number of bytes received.
+ *  @returns		ENOTSOCK if the socket is not found.
+ *  @returns		NO_DATA if there are no received packets or data.
+ *  @returns		ENOMEM if there is not enough memory left.
+ *  @returns		EINVAL if the received address is not an IP address.
+ *  @returns		Other error codes as defined for the packet_translate()
+ *			function.
+ *  @returns		Other error codes as defined for the data_reply()
+ *			function.
+ */
+int
+udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
+    size_t * addrlen);
 
 /*@}*/
@@ -189,10 +214,20 @@
 /** UDP global data.
  */
-udp_globals_t	udp_globals;
-
-int udp_initialize(async_client_conn_t client_connection){
+udp_globals_t udp_globals;
+
+int udp_initialize(async_client_conn_t client_connection)
+{
 	ERROR_DECLARE;
 
-	measured_string_t names[] = {{str_dup("UDP_CHECKSUM_COMPUTING"), 22}, {str_dup("UDP_AUTOBINDING"), 15}};
+	measured_string_t names[] = {
+		{
+			str_dup("UDP_CHECKSUM_COMPUTING"),
+			22
+		},
+		{
+			str_dup("UDP_AUTOBINDING"),
+			15
+		}
+	};
 	measured_string_ref configuration;
 	size_t count = sizeof(names) / sizeof(measured_string_t);
@@ -201,13 +236,18 @@
 	fibril_rwlock_initialize(&udp_globals.lock);
 	fibril_rwlock_write_lock(&udp_globals.lock);
-	udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
-	udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg);
-	if(udp_globals.ip_phone < 0){
+
+	udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
+	    ICMP_CONNECT_TIMEOUT);
+	udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP,
+	    SERVICE_UDP, client_connection, udp_received_msg);
+	if (udp_globals.ip_phone < 0)
 		return udp_globals.ip_phone;
-	}
+
 	// read default packet dimensions
-	ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, &udp_globals.packet_dimension));
+	ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
+	    &udp_globals.packet_dimension));
 	ERROR_PROPAGATE(socket_ports_initialize(&udp_globals.sockets));
-	if(ERROR_OCCURRED(packet_dimensions_initialize(&udp_globals.dimensions))){
+	if (ERROR_OCCURRED(packet_dimensions_initialize(
+	    &udp_globals.dimensions))) {
 		socket_ports_destroy(&udp_globals.sockets);
 		return ERROR_CODE;
@@ -216,35 +256,43 @@
 	udp_globals.packet_dimension.content -= sizeof(udp_header_t);
 	udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
+
 	// get configuration
 	udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
 	udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;
 	configuration = &names[0];
-	ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration, count, &data));
-	if(configuration){
-		if(configuration[0].value){
-			udp_globals.checksum_computing = (configuration[0].value[0] == 'y');
-		}
-		if(configuration[1].value){
-			udp_globals.autobinding = (configuration[1].value[0] == 'y');
-		}
+	ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration,
+	    count, &data));
+	if (configuration) {
+		if (configuration[0].value)
+			udp_globals.checksum_computing =
+			    (configuration[0].value[0] == 'y');
+		
+		if (configuration[1].value)
+			udp_globals.autobinding =
+			    (configuration[1].value[0] == 'y');
+
 		net_free_settings(configuration, data);
 	}
+
 	fibril_rwlock_write_unlock(&udp_globals.lock);
 	return EOK;
 }
 
-int udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){
+int
+udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
+    services_t error)
+{
 	int result;
 
 	fibril_rwlock_write_lock(&udp_globals.lock);
 	result = udp_process_packet(device_id, packet, error);
-	if(result != EOK){
+	if (result != EOK)
 		fibril_rwlock_write_unlock(&udp_globals.lock);
-	}
 
 	return result;
 }
 
-int udp_process_packet(device_id_t device_id, packet_t packet, services_t error){
+int udp_process_packet(device_id_t device_id, packet_t packet, services_t error)
+{
 	ERROR_DECLARE;
 
@@ -262,57 +310,57 @@
 	icmp_code_t code;
 	void *ip_header;
-	struct sockaddr * src;
-	struct sockaddr * dest;
+	struct sockaddr *src;
+	struct sockaddr *dest;
 	packet_dimension_ref packet_dimension;
 
-	if(error){
-		switch(error){
-			case SERVICE_ICMP:
-				// ignore error
-				// length = icmp_client_header_length(packet);
-				// process error
-				result = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
-				if(result < 0){
-					return udp_release_and_return(packet, result);
-				}
-				length = (size_t) result;
-				if(ERROR_OCCURRED(packet_trim(packet, length, 0))){
-					return udp_release_and_return(packet, ERROR_CODE);
-				}
-				break;
-			default:
-				return udp_release_and_return(packet, ENOTSUP);
-		}
-	}
+	if (error) {
+		switch (error) {
+		case SERVICE_ICMP:
+			// ignore error
+			// length = icmp_client_header_length(packet);
+			// process error
+			result = icmp_client_process_packet(packet, &type,
+			    &code, NULL, NULL);
+			if (result < 0)
+				return udp_release_and_return(packet, result);
+			length = (size_t) result;
+			if (ERROR_OCCURRED(packet_trim(packet, length, 0)))
+				return udp_release_and_return(packet,
+				    ERROR_CODE);
+			break;
+		default:
+			return udp_release_and_return(packet, ENOTSUP);
+		}
+	}
+
 	// TODO process received ipopts?
 	result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
-	if(result < 0){
+	if (result < 0)
 		return udp_release_and_return(packet, result);
-	}
 	offset = (size_t) result;
 
 	length = packet_get_data_length(packet);
-	if(length <= 0){
+	if (length <= 0)
 		return udp_release_and_return(packet, EINVAL);
-	}
-	if(length < UDP_HEADER_SIZE + offset){
+	if (length < UDP_HEADER_SIZE + offset)
 		return udp_release_and_return(packet, NO_DATA);
-	}
 
 	// trim all but UDP header
-	if(ERROR_OCCURRED(packet_trim(packet, offset, 0))){
+	if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
 		return udp_release_and_return(packet, ERROR_CODE);
-	}
 
 	// get udp header
 	header = (udp_header_ref) packet_get_data(packet);
-	if(! header){
+	if (!header)
 		return udp_release_and_return(packet, NO_DATA);
-	}
+
 	// find the destination socket
-	socket = socket_port_find(&udp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
-	if(! socket){
-		if(tl_prepare_icmp_packet(udp_globals.net_phone, udp_globals.icmp_phone, packet, error) == EOK){
-			icmp_destination_unreachable_msg(udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet);
+	socket = socket_port_find(&udp_globals.sockets,
+	ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
+	if (!socket) {
+		if (tl_prepare_icmp_packet(udp_globals.net_phone,
+		    udp_globals.icmp_phone, packet, error) == EOK) {
+			icmp_destination_unreachable_msg(udp_globals.icmp_phone,
+			    ICMP_PORT_UNREACH, 0, packet);
 		}
 		return EADDRNOTAVAIL;
@@ -323,59 +371,81 @@
 	fragments = 0;
 	total_length = ntohs(header->total_length);
+
 	// compute header checksum if set
-	if(header->checksum && (! error)){
-		result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
-		if(result <= 0){
+	if (header->checksum && (!error)) {
+		result = packet_get_addr(packet, (uint8_t **) &src,
+		    (uint8_t **) &dest);
+		if( result <= 0)
 			return udp_release_and_return(packet, result);
-		}
-		if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest, result, total_length, &ip_header, &length))){
+
+		if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP,
+		    src, result, dest, result, total_length, &ip_header,
+		    &length))) {
 			return udp_release_and_return(packet, ERROR_CODE);
-		}else{
+		} else {
 			checksum = compute_checksum(0, ip_header, length);
-			// the udp header checksum will be added with the first fragment later
+			// the udp header checksum will be added with the first
+			// fragment later
 			free(ip_header);
 		}
-	}else{
+	} else {
 		header->checksum = 0;
 		checksum = 0;
 	}
 
-	do{
+	do {
 		++ fragments;
 		length = packet_get_data_length(next_packet);
-		if(length <= 0){
+		if (length <= 0)
 			return udp_release_and_return(packet, NO_DATA);
-		}
-		if(total_length < length){
-			if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - total_length))){
-				return udp_release_and_return(packet, ERROR_CODE);
+
+		if (total_length < length) {
+			if (ERROR_OCCURRED(packet_trim(next_packet, 0,
+			    length - total_length))) {
+				return udp_release_and_return(packet,
+				    ERROR_CODE);
 			}
+
 			// add partial checksum if set
-			if(header->checksum){
-				checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
+			if (header->checksum) {
+				checksum = compute_checksum(checksum,
+				    packet_get_data(packet),
+				    packet_get_data_length(packet));
 			}
+
 			// relese the rest of the packet fragments
 			tmp_packet = pq_next(next_packet);
-			while(tmp_packet){
+			while (tmp_packet) {
 				next_packet = pq_detach(tmp_packet);
-				pq_release_remote(udp_globals.net_phone, packet_get_id(tmp_packet));
+				pq_release_remote(udp_globals.net_phone,
+				    packet_get_id(tmp_packet));
 				tmp_packet = next_packet;
 			}
+
 			// exit the loop
 			break;
 		}
 		total_length -= length;
+
 		// add partial checksum if set
-		if(header->checksum){
-			checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
-		}
-	}while((next_packet = pq_next(next_packet)) && (total_length > 0));
+		if (header->checksum) {
+			checksum = compute_checksum(checksum,
+			    packet_get_data(packet),
+			    packet_get_data_length(packet));
+		}
+
+	} while ((next_packet = pq_next(next_packet)) && (total_length > 0));
 
 	// check checksum
-	if(header->checksum){
-		if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){
-			if(tl_prepare_icmp_packet(udp_globals.net_phone, udp_globals.icmp_phone, packet, error) == EOK){
+	if (header->checksum) {
+		if (flip_checksum(compact_checksum(checksum)) !=
+		    IP_CHECKSUM_ZERO) {
+			if (tl_prepare_icmp_packet(udp_globals.net_phone,
+			    udp_globals.icmp_phone, packet, error) == EOK) {
 				// checksum error ICMP
-				icmp_parameter_problem_msg(udp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet);
+				icmp_parameter_problem_msg(
+				    udp_globals.icmp_phone, ICMP_PARAM_POINTER,
+				    ((size_t) ((void *) &header->checksum)) -
+				    ((size_t) ((void *) header)), packet);
 			}
 			return EINVAL;
@@ -384,6 +454,8 @@
 
 	// queue the received packet
-	if(ERROR_OCCURRED(dyn_fifo_push(&socket->received, packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE))
-	    || ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, device_id, &packet_dimension))){
+	if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
+	    packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
+	    ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone,
+	    &udp_globals.dimensions, device_id, &packet_dimension))) {
 		return udp_release_and_return(packet, ERROR_CODE);
 	}
@@ -391,9 +463,15 @@
 	// notify the destination socket
 	fibril_rwlock_write_unlock(&udp_globals.lock);
-	async_msg_5(socket->phone, NET_SOCKET_RECEIVED, (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0, (ipcarg_t) fragments);
+	async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
+	    (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0,
+	    (ipcarg_t) fragments);
+
 	return EOK;
 }
 
-int udp_message_standalone(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
+int
+udp_message_standalone(ipc_callid_t callid, ipc_call_t * call,
+    ipc_call_t * answer, int * answer_count)
+{
 	ERROR_DECLARE;
 
@@ -401,22 +479,28 @@
 
 	*answer_count = 0;
-	switch(IPC_GET_METHOD(*call)){
-		case NET_TL_RECEIVED:
-			if(! ERROR_OCCURRED(packet_translate_remote(udp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
-				ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_UDP, IPC_GET_ERROR(call));
-			}
-			return ERROR_CODE;
-		case IPC_M_CONNECT_TO_ME:
-			return udp_process_client_messages(callid, * call);
-	}
+
+	switch (IPC_GET_METHOD(*call)) {
+	case NET_TL_RECEIVED:
+		if (!ERROR_OCCURRED(packet_translate_remote(
+		    udp_globals.net_phone, &packet, IPC_GET_PACKET(call)))) {
+			ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call),
+			    packet, SERVICE_UDP, IPC_GET_ERROR(call));
+		}
+		return ERROR_CODE;
+	
+	case IPC_M_CONNECT_TO_ME:
+		return udp_process_client_messages(callid, * call);
+	}
+
 	return ENOTSUP;
 }
 
-int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call){
+int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
+{
 	int res;
 	bool keep_on_going = true;
 	socket_cores_t local_sockets;
 	int app_phone = IPC_GET_PHONE(&call);
-	struct sockaddr * addr;
+	struct sockaddr *addr;
 	int socket_id;
 	size_t addrlen;
@@ -433,9 +517,10 @@
 	answer_count = 0;
 
-	// The client connection is only in one fibril and therefore no additional locks are needed.
+	// The client connection is only in one fibril and therefore no
+	// additional locks are needed.
 
 	socket_cores_initialize(&local_sockets);
 
-	while(keep_on_going){
+	while (keep_on_going) {
 
 		// answer the call
@@ -449,67 +534,94 @@
 
 		// process the call
-		switch(IPC_GET_METHOD(call)){
-			case IPC_M_PHONE_HUNGUP:
-				keep_on_going = false;
-				res = EHANGUP;
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			keep_on_going = false;
+			res = EHANGUP;
+			break;
+
+		case NET_SOCKET:
+			socket_id = SOCKET_GET_SOCKET_ID(call);
+			res = socket_create(&local_sockets, app_phone, NULL,
+			    &socket_id);
+			SOCKET_SET_SOCKET_ID(answer, socket_id);
+
+			if (res != EOK)
 				break;
-			case NET_SOCKET:
-				socket_id = SOCKET_GET_SOCKET_ID(call);
-				res = socket_create(&local_sockets, app_phone, NULL, &socket_id);
-				SOCKET_SET_SOCKET_ID(answer, socket_id);
-
-				if(res == EOK){
-					if (tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){
-						SOCKET_SET_DATA_FRAGMENT_SIZE(answer, packet_dimension->content);
-					}
-//					SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_UDP_FRAGMENT_SIZE);
-					SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
-					answer_count = 3;
-				}
+			
+			if (tl_get_ip_packet_dimension(udp_globals.ip_phone,
+			    &udp_globals.dimensions, DEVICE_INVALID_ID,
+			    &packet_dimension) == EOK) {
+				SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
+				    packet_dimension->content);
+			}
+
+//			SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
+//			    MAX_UDP_FRAGMENT_SIZE);
+			SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
+			answer_count = 3;
+			break;
+
+		case NET_SOCKET_BIND:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
 				break;
-			case NET_SOCKET_BIND:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					fibril_rwlock_write_lock(&udp_globals.lock);
-					res = socket_bind(&local_sockets, &udp_globals.sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port);
-					fibril_rwlock_write_unlock(&udp_globals.lock);
-					free(addr);
-				}
+			fibril_rwlock_write_lock(&udp_globals.lock);
+			res = socket_bind(&local_sockets, &udp_globals.sockets,
+			    SOCKET_GET_SOCKET_ID(call), addr, addrlen,
+			    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
+			    udp_globals.last_used_port);
+			fibril_rwlock_write_unlock(&udp_globals.lock);
+			free(addr);
+			break;
+
+		case NET_SOCKET_SENDTO:
+			res = data_receive((void **) &addr, &addrlen);
+			if (res != EOK)
 				break;
-			case NET_SOCKET_SENDTO:
-				res = data_receive((void **) &addr, &addrlen);
-				if(res == EOK){
-					fibril_rwlock_write_lock(&udp_globals.lock);
-					res = udp_sendto_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
-					SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
-					if(res != EOK){
-						fibril_rwlock_write_unlock(&udp_globals.lock);
-					}else{
-						answer_count = 2;
-					}
-					free(addr);
-				}
+
+			fibril_rwlock_write_lock(&udp_globals.lock);
+			res = udp_sendto_message(&local_sockets,
+			    SOCKET_GET_SOCKET_ID(call), addr, addrlen,
+			    SOCKET_GET_DATA_FRAGMENTS(call), &size,
+			    SOCKET_GET_FLAGS(call));
+			SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
+
+			if (res != EOK)
+				fibril_rwlock_write_unlock(&udp_globals.lock);
+			else
+				answer_count = 2;
+			
+			free(addr);
+			break;
+
+		case NET_SOCKET_RECVFROM:
+			fibril_rwlock_write_lock(&udp_globals.lock);
+			res = udp_recvfrom_message(&local_sockets,
+			     SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
+			     &addrlen);
+			fibril_rwlock_write_unlock(&udp_globals.lock);
+
+			if (res <= 0)
 				break;
-			case NET_SOCKET_RECVFROM:
-				fibril_rwlock_write_lock(&udp_globals.lock);
-				res = udp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen);
-				fibril_rwlock_write_unlock(&udp_globals.lock);
-				if(res > 0){
-					SOCKET_SET_READ_DATA_LENGTH(answer, res);
-					SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
-					answer_count = 3;
-					res = EOK;
-				}
-				break;
-			case NET_SOCKET_CLOSE:
-				fibril_rwlock_write_lock(&udp_globals.lock);
-				res = socket_destroy(udp_globals.net_phone, SOCKET_GET_SOCKET_ID(call), &local_sockets, &udp_globals.sockets, NULL);
-				fibril_rwlock_write_unlock(&udp_globals.lock);
-				break;
-			case NET_SOCKET_GETSOCKOPT:
-			case NET_SOCKET_SETSOCKOPT:
-			default:
-				res = ENOTSUP;
-				break;
+
+			SOCKET_SET_READ_DATA_LENGTH(answer, res);
+			SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
+			answer_count = 3;
+			res = EOK;
+			break;
+			
+		case NET_SOCKET_CLOSE:
+			fibril_rwlock_write_lock(&udp_globals.lock);
+			res = socket_destroy(udp_globals.net_phone,
+			    SOCKET_GET_SOCKET_ID(call), &local_sockets,
+			    &udp_globals.sockets, NULL);
+			fibril_rwlock_write_unlock(&udp_globals.lock);
+			break;
+
+		case NET_SOCKET_GETSOCKOPT:
+		case NET_SOCKET_SETSOCKOPT:
+		default:
+			res = ENOTSUP;
+			break;
 		}
 	}
@@ -519,10 +631,15 @@
 
 	// release all local sockets
-	socket_cores_release(udp_globals.net_phone, &local_sockets, &udp_globals.sockets, NULL);
+	socket_cores_release(udp_globals.net_phone, &local_sockets,
+	    &udp_globals.sockets, NULL);
 
 	return res;
 }
 
-int udp_sendto_message(socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags){
+int
+udp_sendto_message(socket_cores_ref local_sockets, int socket_id,
+    const struct sockaddr *addr, socklen_t addrlen, int fragments,
+    size_t *data_fragment_size, int flags)
+{
 	ERROR_DECLARE;
 
@@ -544,22 +661,27 @@
 
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
-
-	if((socket->port <= 0) && udp_globals.autobinding){
+
+	if ((socket->port <= 0) && udp_globals.autobinding) {
 		// bind the socket to a random free port if not bound
-//		do{
+//		do {
 			// try to find a free port
 //			fibril_rwlock_read_unlock(&udp_globals.lock);
 //			fibril_rwlock_write_lock(&udp_globals.lock);
 			// might be changed in the meantime
-//			if(socket->port <= 0){
-				if(ERROR_OCCURRED(socket_bind_free_port(&udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port))){
-//					fibril_rwlock_write_unlock(&udp_globals.lock);
-//					fibril_rwlock_read_lock(&udp_globals.lock);
+//			if (socket->port <= 0) {
+				if (ERROR_OCCURRED(socket_bind_free_port(
+				    &udp_globals.sockets, socket,
+				    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
+				    udp_globals.last_used_port))) {
+//					fibril_rwlock_write_unlock(
+//					    &udp_globals.lock);
+//					fibril_rwlock_read_lock(
+//					    &udp_globals.lock);
 					return ERROR_CODE;
 				}
-				// set the next port as the search starting port number
+				// set the next port as the search starting port
+				// number
 				udp_globals.last_used_port = socket->port;
 //			}
@@ -567,51 +689,61 @@
 //			fibril_rwlock_read_lock(&udp_globals.lock);
 			// might be changed in the meantime
-//		}while(socket->port <= 0);
-	}
-
-	if(udp_globals.checksum_computing){
-		if(ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr, addrlen, &device_id, &ip_header, &headerlen))){
+//		} while (socket->port <= 0);
+	}
+
+	if (udp_globals.checksum_computing) {
+		if (ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone,
+		    IPPROTO_UDP, addr, addrlen, &device_id, &ip_header,
+		    &headerlen))) {
 			return udp_release_and_return(packet, ERROR_CODE);
 		}
 		// get the device packet dimension
-//		ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, device_id, &packet_dimension));
-	}
-//	}else{
+//		ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone,
+//		    &udp_globals.dimensions, device_id, &packet_dimension));
+	}
+//	} else {
 		// do not ask all the time
-		ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, &udp_globals.packet_dimension));
+		ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
+		    &udp_globals.packet_dimension));
 		packet_dimension = &udp_globals.packet_dimension;
 //	}
 
 	// read the first packet fragment
-	result = tl_socket_read_packet_data(udp_globals.net_phone, &packet, UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
-	if(result < 0){
+	result = tl_socket_read_packet_data(udp_globals.net_phone, &packet,
+	    UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
+	if (result < 0)
 		return result;
-	}
+
 	total_length = (size_t) result;
-	if(udp_globals.checksum_computing){
-		checksum = compute_checksum(0, packet_get_data(packet), packet_get_data_length(packet));
-	}else{
+	if (udp_globals.checksum_computing)
+		checksum = compute_checksum(0, packet_get_data(packet),
+		    packet_get_data_length(packet));
+	else
 		checksum = 0;
-	}
+
 	// prefix the udp header
 	header = PACKET_PREFIX(packet, udp_header_t);
-	if(! header){
+	if(! header)
 		return udp_release_and_return(packet, ENOMEM);
-	}
+
 	bzero(header, sizeof(*header));
 	// read the rest of the packet fragments
-	for(index = 1; index < fragments; ++ index){
-		result = tl_socket_read_packet_data(udp_globals.net_phone, &next_packet, 0, packet_dimension, addr, addrlen);
-		if(result < 0){
+	for (index = 1; index < fragments; ++ index) {
+		result = tl_socket_read_packet_data(udp_globals.net_phone,
+		    &next_packet, 0, packet_dimension, addr, addrlen);
+		if (result < 0)
 			return udp_release_and_return(packet, result);
-		}
-		if(ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0))){
+
+		if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0)))
 			return udp_release_and_return(packet, ERROR_CODE);
-		}
+
 		total_length += (size_t) result;
-		if(udp_globals.checksum_computing){
-			checksum = compute_checksum(checksum, packet_get_data(next_packet), packet_get_data_length(next_packet));
-		}
-	}
+		if (udp_globals.checksum_computing) {
+			checksum = compute_checksum(checksum,
+			    packet_get_data(next_packet),
+			    packet_get_data_length(next_packet));
+		}
+	}
+
 	// set the udp header
 	header->source_port = htons((socket->port > 0) ? socket->port : 0);
@@ -619,29 +751,40 @@
 	header->total_length = htons(total_length + sizeof(*header));
 	header->checksum = 0;
-	if(udp_globals.checksum_computing){
+	if (udp_globals.checksum_computing) {
 		// update the pseudo header
-		if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(ip_header, headerlen, total_length + UDP_HEADER_SIZE))){
+		if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
+		    ip_header, headerlen, total_length + UDP_HEADER_SIZE))) {
 			free(ip_header);
 			return udp_release_and_return(packet, ERROR_CODE);
 		}
+
 		// finish the checksum computation
 		checksum = compute_checksum(checksum, ip_header, headerlen);
-		checksum = compute_checksum(checksum, (uint8_t *) header, sizeof(*header));
-		header->checksum = htons(flip_checksum(compact_checksum(checksum)));
+		checksum = compute_checksum(checksum, (uint8_t *) header,
+		    sizeof(*header));
+		header->checksum =
+		    htons(flip_checksum(compact_checksum(checksum)));
 		free(ip_header);
-	}else{
+	} else {
 		device_id = DEVICE_INVALID_ID;
 	}
+
 	// prepare the first packet fragment
-	if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0))){
+	if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0,
+	    0, 0))) {
 		return udp_release_and_return(packet, ERROR_CODE);
 	}
+
 	// send the packet
 	fibril_rwlock_write_unlock(&udp_globals.lock);
 	ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
+
 	return EOK;
 }
 
-int udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){
+int
+udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
+    size_t *addrlen)
+{
 	ERROR_DECLARE;
 
@@ -650,23 +793,25 @@
 	packet_t packet;
 	udp_header_ref header;
-	struct sockaddr * addr;
+	struct sockaddr *addr;
 	size_t length;
-	uint8_t * data;
+	uint8_t *data;
 	int result;
 
 	// find the socket
 	socket = socket_cores_find(local_sockets, socket_id);
-	if(! socket){
+	if (!socket)
 		return ENOTSOCK;
-	}
+
 	// get the next received packet
 	packet_id = dyn_fifo_value(&socket->received);
-	if(packet_id < 0){
+	if (packet_id < 0)
 		return NO_DATA;
-	}
-	ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet, packet_id));
+
+	ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet,
+	    packet_id));
+
 	// get udp header
 	data = packet_get_data(packet);
-	if(! data){
+	if (!data) {
 		pq_release_remote(udp_globals.net_phone, packet_id);
 		return NO_DATA;
@@ -676,9 +821,11 @@
 	// set the source address port
 	result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
-	if(ERROR_OCCURRED(tl_set_address_port(addr, result, ntohs(header->source_port)))){
+	if (ERROR_OCCURRED(tl_set_address_port(addr, result,
+	    ntohs(header->source_port)))) {
 		pq_release_remote(udp_globals.net_phone, packet_id);
 		return ERROR_CODE;
 	}
 	*addrlen = (size_t) result;
+
 	// send the source address
 	ERROR_PROPAGATE(data_reply(addr, * addrlen));
@@ -693,9 +840,11 @@
 	dyn_fifo_pop(&socket->received);
 	pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
+
 	// return the total length
 	return (int) length;
 }
 
-int udp_release_and_return(packet_t packet, int result){
+int udp_release_and_return(packet_t packet, int result)
+{
 	pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
 	return result;
@@ -704,6 +853,6 @@
 /** Default thread for new connections.
  *
- *  @param[in] iid The initial message identifier.
- *  @param[in] icall The initial message call structure.
+ *  @param[in] iid	The initial message identifier.
+ *  @param[in] icall	The initial message call structure.
  *
  */
@@ -716,5 +865,5 @@
 	ipc_answer_0(iid, EOK);
 	
-	while(true) {
+	while (true) {
 		ipc_call_t answer;
 		int answer_count;
@@ -731,6 +880,9 @@
 		    &answer_count);
 		
-		/* End if said to either by the message or the processing result */
-		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
+		/*
+		 * End if said to either by the message or the processing result
+		 */
+		if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
+		    (res == EHANGUP))
 			return;
 		
@@ -742,10 +894,11 @@
 /** Starts the module.
  *
- *  @param argc The count of the command line arguments. Ignored parameter.
- *  @param argv The command line parameters. Ignored parameter.
- *
- *  @returns EOK on success.
- *  @returns Other error codes as defined for each specific module start function.
- *
+ *  @param argc		The count of the command line arguments. Ignored
+ *			parameter.
+ *  @param argv		The command line parameters. Ignored parameter.
+ *
+ *  @returns		EOK on success.
+ *  @returns		Other error codes as defined for each specific module
+ *			start function.
  */
 int main(int argc, char *argv[])
Index: uspace/srv/net/tl/udp/udp_module.c
===================================================================
--- uspace/srv/net/tl/udp/udp_module.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/net/tl/udp/udp_module.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -40,8 +40,8 @@
 #include <async.h>
 #include <stdio.h>
+#include <err.h>
 #include <ipc/ipc.h>
 #include <ipc/services.h>
 
-#include <net_err.h>
 #include <net_modules.h>
 #include <packet/packet.h>
Index: uspace/srv/vfs/vfs.h
===================================================================
--- uspace/srv/vfs/vfs.h	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs.h	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -169,5 +169,5 @@
 
 extern int vfs_grab_phone(fs_handle_t);
-extern void vfs_release_phone(int);
+extern void vfs_release_phone(fs_handle_t, int);
 
 extern fs_handle_t fs_name_to_handle(char *, bool);
Index: uspace/srv/vfs/vfs_lookup.c
===================================================================
--- uspace/srv/vfs/vfs_lookup.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs_lookup.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -168,5 +168,5 @@
 	ipcarg_t rc;
 	async_wait_for(req, &rc);
-	vfs_release_phone(phone);
+	vfs_release_phone(root->fs_handle, phone);
 	
 	fibril_mutex_lock(&plb_mutex);
@@ -215,5 +215,5 @@
 	ipcarg_t rc;
 	async_wait_for(req, &rc);
-	vfs_release_phone(phone);
+	vfs_release_phone(result->triplet.fs_handle, phone);
 	
 	if (rc == EOK) {
Index: uspace/srv/vfs/vfs_node.c
===================================================================
--- uspace/srv/vfs/vfs_node.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs_node.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -133,5 +133,5 @@
 		    (ipcarg_t)node->dev_handle, (ipcarg_t)node->index);
 		assert(rc == EOK);
-		vfs_release_phone(phone);
+		vfs_release_phone(node->fs_handle, phone);
 	}
 	if (free_vfs_node)
Index: uspace/srv/vfs/vfs_ops.c
===================================================================
--- uspace/srv/vfs/vfs_ops.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs_ops.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -131,5 +131,5 @@
 			if (rc != EOK) {
 				async_wait_for(msg, NULL);
-				vfs_release_phone(phone);
+				vfs_release_phone(fs_handle, phone);
 				fibril_rwlock_write_unlock(&namespace_rwlock);
 				ipc_answer_0(rid, rc);
@@ -137,5 +137,5 @@
 			}
 			async_wait_for(msg, &rc);
-			vfs_release_phone(phone);
+			vfs_release_phone(fs_handle, phone);
 			
 			if (rc != EOK) {
@@ -196,6 +196,6 @@
 	if (rc != EOK) {
 		async_wait_for(msg, NULL);
-		vfs_release_phone(mountee_phone);
-		vfs_release_phone(phone);
+		vfs_release_phone(fs_handle, mountee_phone);
+		vfs_release_phone(mp_res.triplet.fs_handle, phone);
 		/* Mount failed, drop reference to mp_node. */
 		if (mp_node)
@@ -206,5 +206,5 @@
 	}
 
-	vfs_release_phone(mountee_phone);
+	vfs_release_phone(fs_handle, mountee_phone);
 	
 	/* send the mount options */
@@ -212,5 +212,5 @@
 	if (rc != EOK) {
 		async_wait_for(msg, NULL);
-		vfs_release_phone(phone);
+		vfs_release_phone(mp_res.triplet.fs_handle, phone);
 		/* Mount failed, drop reference to mp_node. */
 		if (mp_node)
@@ -221,5 +221,5 @@
 	}
 	async_wait_for(msg, &rc);
-	vfs_release_phone(phone);
+	vfs_release_phone(mp_res.triplet.fs_handle, phone);
 	
 	if (rc == EOK) {
@@ -423,5 +423,5 @@
 		rc = async_req_1_0(phone, VFS_OUT_UNMOUNTED,
 		    mr_node->dev_handle);
-		vfs_release_phone(phone);
+		vfs_release_phone(mr_node->fs_handle, phone);
 		if (rc != EOK) {
 			fibril_rwlock_write_unlock(&namespace_rwlock);
@@ -460,5 +460,5 @@
 		rc = async_req_2_0(phone, VFS_OUT_UNMOUNT, mp_node->dev_handle,
 		    mp_node->index);
-		vfs_release_phone(phone);
+		vfs_release_phone(mp_node->fs_handle, phone);
 		if (rc != EOK) {
 			fibril_rwlock_write_unlock(&namespace_rwlock);
@@ -716,5 +716,5 @@
 	async_wait_for(msg, &rc);
 	
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(file->node->fs_handle, fs_phone);
 	fibril_mutex_unlock(&file->lock);
 	
@@ -747,5 +747,5 @@
 		async_wait_for(msg, &rc);
 		
-		vfs_release_phone(fs_phone);
+		vfs_release_phone(file->node->fs_handle, fs_phone);
 		fibril_mutex_unlock(&file->lock);
 		
@@ -846,5 +846,5 @@
 	}
 	
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(file->node->fs_handle, fs_phone);
 	
 	size_t bytes = IPC_GET_ARG1(answer);
@@ -970,5 +970,5 @@
 	rc = async_req_4_0(fs_phone, VFS_OUT_TRUNCATE, (ipcarg_t) dev_handle,
 	    (ipcarg_t) index, LOWER32(size), UPPER32(size));
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(fs_handle, fs_phone);
 	return (int)rc;
 }
@@ -1026,5 +1026,5 @@
 	ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
 	async_wait_for(msg, &rc);
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(file->node->fs_handle, fs_phone);
 
 	fibril_mutex_unlock(&file->lock);
@@ -1077,5 +1077,5 @@
 	ipcarg_t rv;
 	async_wait_for(msg, &rv);
-	vfs_release_phone(fs_phone);
+	vfs_release_phone(node->fs_handle, fs_phone);
 
 	ipc_answer_0(rid, rv);
Index: uspace/srv/vfs/vfs_register.c
===================================================================
--- uspace/srv/vfs/vfs_register.c	(revision 18825251b8c3e8e97adbfdaa2ea5343181835914)
+++ uspace/srv/vfs/vfs_register.c	(revision 49d871eaf16d7d7ba25420bf70734ff9633219d6)
@@ -39,5 +39,7 @@
 #include <ipc/services.h>
 #include <async.h>
+#include <async_rel.h>
 #include <fibril.h>
+#include <fibril_synch.h>
 #include <errno.h>
 #include <stdio.h>
@@ -46,5 +48,4 @@
 #include <ctype.h>
 #include <bool.h>
-#include <fibril_synch.h>
 #include <adt/list.h>
 #include <as.h>
@@ -252,4 +253,6 @@
 int vfs_grab_phone(fs_handle_t handle)
 {
+	link_t *cur;
+	fs_info_t *fs;
 	int phone;
 
@@ -262,6 +265,4 @@
 	 */
 	fibril_mutex_lock(&fs_head_lock);
-	link_t *cur;
-	fs_info_t *fs;
 	for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
 		fs = list_get_instance(cur, fs_info_t, fs_link);
@@ -269,5 +270,5 @@
 			fibril_mutex_unlock(&fs_head_lock);
 			fibril_mutex_lock(&fs->phone_lock);
-			phone = ipc_connect_me_to(fs->phone, 0, 0, 0);
+			phone = async_relation_create(fs->phone);
 			fibril_mutex_unlock(&fs->phone_lock);
 
@@ -284,8 +285,23 @@
  * @param phone		Phone to FS task.
  */
-void vfs_release_phone(int phone)
-{
-	/* TODO: implement connection caching */
-	ipc_hangup(phone);
+void vfs_release_phone(fs_handle_t handle, int phone)
+{
+	link_t *cur;
+	fs_info_t *fs;
+
+	fibril_mutex_lock(&fs_head_lock);
+	for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
+		fs = list_get_instance(cur, fs_info_t, fs_link);
+		if (fs->fs_handle == handle) {
+			fibril_mutex_unlock(&fs_head_lock);
+			fibril_mutex_lock(&fs->phone_lock);
+			async_relation_destroy(fs->phone, phone);
+			fibril_mutex_unlock(&fs->phone_lock);
+			return;
+		}
+	}
+	/* should not really get here */
+	abort();
+	fibril_mutex_unlock(&fs_head_lock);
 }
 
