Index: uspace/app/netecho/netecho.c
===================================================================
--- uspace/app/netecho/netecho.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/app/netecho/netecho.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -243,5 +243,5 @@
 		if (verbose)
 			printf("accept()\n");
-            	socket_id = accept(listening_id, (void *) address_buf, &addrlen);
+		socket_id = accept(listening_id, (void *) address_buf, &addrlen);
 		if (socket_id <= 0) {
 			socket_print_error(stderr, socket_id, "Socket accept: ", "\n");
@@ -312,5 +312,5 @@
 				rc = sendto(socket_id, reply ? reply : data, reply ? reply_length : length, 0, address, addrlen);
 				if (rc != EOK)
-					socket_print_error(stderr, rc, "Socket send: ", "\n");
+					socket_print_error(stderr, rc, "Socket sendto: ", "\n");
 			}
 		}
@@ -394,5 +394,12 @@
 		return listening_id;
 	}
-
+	
+	/* Bind the listening socket */
+	rc = bind(listening_id, address, addrlen);
+	if (rc != EOK) {
+		socket_print_error(stderr, rc, "Socket bind: ", "\n");
+		return rc;
+	}
+	
 	/* if the stream socket is used */
 	if (type == SOCK_STREAM) {
@@ -402,5 +409,5 @@
 			backlog = 3;
 		}
-
+		
 		/* Set the backlog */
 		rc = listen(listening_id, backlog);
@@ -409,11 +416,4 @@
 			return rc;
 		}
-	}
-
-	/* Bind the listening socket */
-	rc = bind(listening_id, address, addrlen);
-	if (rc != EOK) {
-		socket_print_error(stderr, rc, "Socket bind: ", "\n");
-		return rc;
 	}
 
Index: uspace/app/nettest3/nettest3.c
===================================================================
--- uspace/app/nettest3/nettest3.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/app/nettest3/nettest3.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -63,5 +63,5 @@
 	port = 7;
 
-	data = (char *)"Hello World!";
+	data = (char *) "Hello World!";
 	size = str_size(data);
 
@@ -98,5 +98,5 @@
 
 	printf("connect()\n");
-	rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
+	rc = connect(fd, (struct sockaddr *) &addr, sizeof(addr));
 	printf(" -> %d\n", rc);
 	if (rc != 0)
@@ -115,5 +115,5 @@
 	} while (rc > 0);
 
-	async_usleep(1000*1000);
+	async_usleep(1000 * 1000);
 
 	printf("closesocket()\n");
@@ -124,5 +124,4 @@
 }
 
-
 /** @}
  */
Index: uspace/lib/c/generic/async.c
===================================================================
--- uspace/lib/c/generic/async.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/lib/c/generic/async.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -2068,4 +2068,5 @@
 	
 	async_sess_t *sess = exch->sess;
+	assert(sess != NULL);
 	
 	atomic_dec(&sess->refcnt);
Index: uspace/lib/c/generic/net/socket_client.c
===================================================================
--- uspace/lib/c/generic/net/socket_client.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/lib/c/generic/net/socket_client.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -192,84 +192,88 @@
 /** Default thread for new connections.
  *
- * @param[in] iid	The initial message identifier.
- * @param[in] icall	The initial message call structure.
- * @param[in] arg	Local argument.
+ * @param[in] iid   The initial message identifier.
+ * @param[in] icall The initial message call structure.
+ * @param[in] arg   Local argument.
+ *
  */
 static void socket_connection(ipc_callid_t iid, ipc_call_t * icall, void *arg)
 {
-	ipc_callid_t callid;
-	ipc_call_t call;
-	socket_t *socket;
-	int rc;
-
-loop:
-	callid = async_get_call(&call);
-
-	switch (IPC_GET_IMETHOD(call)) {
-	case NET_SOCKET_RECEIVED:
-	case NET_SOCKET_ACCEPTED:
-	case NET_SOCKET_DATA_FRAGMENT_SIZE:
-		fibril_rwlock_read_lock(&socket_globals.lock);
-
-		/* Find the socket */
-		socket = sockets_find(socket_get_sockets(),
-		    SOCKET_GET_SOCKET_ID(call));
-		if (!socket) {
-			rc = ENOTSOCK;
-			fibril_rwlock_read_unlock(&socket_globals.lock);
-			break;
+	while (true) {
+		ipc_call_t call;
+		ipc_callid_t callid = async_get_call(&call);
+		
+		if (!IPC_GET_IMETHOD(call)) {
+			async_answer_0(callid, 0);
+			return;
 		}
+		
+		int rc;
 		
 		switch (IPC_GET_IMETHOD(call)) {
 		case NET_SOCKET_RECEIVED:
-			fibril_mutex_lock(&socket->receive_lock);
-			/* Push the number of received packet fragments */
-			rc = dyn_fifo_push(&socket->received,
-			    SOCKET_GET_DATA_FRAGMENTS(call),
-			    SOCKET_MAX_RECEIVED_SIZE);
-			if (rc == EOK) {
-				/* Signal the received packet */
-				fibril_condvar_signal(&socket->receive_signal);
+		case NET_SOCKET_ACCEPTED:
+		case NET_SOCKET_DATA_FRAGMENT_SIZE:
+			fibril_rwlock_read_lock(&socket_globals.lock);
+			
+			/* Find the socket */
+			socket_t *socket = sockets_find(socket_get_sockets(),
+			    SOCKET_GET_SOCKET_ID(call));
+			if (!socket) {
+				rc = ENOTSOCK;
+				fibril_rwlock_read_unlock(&socket_globals.lock);
+				break;
 			}
-			fibril_mutex_unlock(&socket->receive_lock);
+			
+			switch (IPC_GET_IMETHOD(call)) {
+			case NET_SOCKET_RECEIVED:
+				fibril_mutex_lock(&socket->receive_lock);
+				/* Push the number of received packet fragments */
+				rc = dyn_fifo_push(&socket->received,
+				    SOCKET_GET_DATA_FRAGMENTS(call),
+				    SOCKET_MAX_RECEIVED_SIZE);
+				if (rc == EOK) {
+					/* Signal the received packet */
+					fibril_condvar_signal(&socket->receive_signal);
+				}
+				fibril_mutex_unlock(&socket->receive_lock);
+				break;
+				
+			case NET_SOCKET_ACCEPTED:
+				/* Push the new socket identifier */
+				fibril_mutex_lock(&socket->accept_lock);
+				rc = dyn_fifo_push(&socket->accepted, 1,
+				    SOCKET_MAX_ACCEPTED_SIZE);
+				if (rc == EOK) {
+					/* Signal the accepted socket */
+					fibril_condvar_signal(&socket->accept_signal);
+				}
+				fibril_mutex_unlock(&socket->accept_lock);
+				break;
+			
+			default:
+				rc = ENOTSUP;
+			}
+			
+			if ((SOCKET_GET_DATA_FRAGMENT_SIZE(call) > 0) &&
+			    (SOCKET_GET_DATA_FRAGMENT_SIZE(call) !=
+			    socket->data_fragment_size)) {
+				fibril_rwlock_write_lock(&socket->sending_lock);
+				
+				/* Set the data fragment size */
+				socket->data_fragment_size =
+				    SOCKET_GET_DATA_FRAGMENT_SIZE(call);
+				
+				fibril_rwlock_write_unlock(&socket->sending_lock);
+			}
+			
+			fibril_rwlock_read_unlock(&socket_globals.lock);
 			break;
-
-		case NET_SOCKET_ACCEPTED:
-			/* Push the new socket identifier */
-			fibril_mutex_lock(&socket->accept_lock);
-			rc = dyn_fifo_push(&socket->accepted, 1,
-			    SOCKET_MAX_ACCEPTED_SIZE);
-			if (rc == EOK) {
-				/* Signal the accepted socket */
-				fibril_condvar_signal(&socket->accept_signal);
-			}
-			fibril_mutex_unlock(&socket->accept_lock);
-			break;
-
+			
 		default:
 			rc = ENOTSUP;
 		}
-
-		if ((SOCKET_GET_DATA_FRAGMENT_SIZE(call) > 0) &&
-		    (SOCKET_GET_DATA_FRAGMENT_SIZE(call) !=
-		    socket->data_fragment_size)) {
-			fibril_rwlock_write_lock(&socket->sending_lock);
-
-			/* Set the data fragment size */
-			socket->data_fragment_size =
-			    SOCKET_GET_DATA_FRAGMENT_SIZE(call);
-
-			fibril_rwlock_write_unlock(&socket->sending_lock);
-		}
-
-		fibril_rwlock_read_unlock(&socket_globals.lock);
-		break;
-
-	default:
-		rc = ENOTSUP;
-	}
-
-	async_answer_0(callid, (sysarg_t) rc);
-	goto loop;
+		
+		async_answer_0(callid, (sysarg_t) rc);
+	}
 }
 
Index: uspace/srv/net/tcp/conn.c
===================================================================
--- uspace/srv/net/tcp/conn.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/srv/net/tcp/conn.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -354,13 +354,19 @@
 {
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_conn_find_ref(%p)", sp);
-
+	
+	log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare conn (f:(%x,%u), l:(%x,%u))",
+	    sp->foreign.addr.ipv4, sp->foreign.port,
+	    sp->local.addr.ipv4, sp->local.port);
+	
 	fibril_mutex_lock(&conn_list_lock);
-
+	
 	list_foreach(conn_list, link) {
 		tcp_conn_t *conn = list_get_instance(link, tcp_conn_t, link);
 		tcp_sockpair_t *csp = &conn->ident;
-		log_msg(LOG_DEFAULT, LVL_DEBUG2, "compare with conn (f:(%x,%u), l:(%x,%u))",
+		
+		log_msg(LOG_DEFAULT, LVL_DEBUG2, " - with (f:(%x,%u), l:(%x,%u))",
 		    csp->foreign.addr.ipv4, csp->foreign.port,
 		    csp->local.addr.ipv4, csp->local.port);
+		
 		if (tcp_sockpair_match(sp, csp)) {
 			tcp_conn_addref(conn);
@@ -369,5 +375,5 @@
 		}
 	}
-
+	
 	fibril_mutex_unlock(&conn_list_lock);
 	return NULL;
Index: uspace/srv/net/tcp/sock.c
===================================================================
--- uspace/srv/net/tcp/sock.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/srv/net/tcp/sock.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -203,36 +203,51 @@
 {
 	int rc;
-	struct sockaddr *addr;
-	size_t addr_len;
+	struct sockaddr_in *addr;
+	size_t addr_size;
 	socket_core_t *sock_core;
 	tcp_sockdata_t *socket;
-
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_bind()");
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");
-	rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_len);
+	
+	addr = NULL;
+	
+	rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);
 	if (rc != EOK) {
 		async_answer_0(callid, rc);
-		return;
-	}
-
+		goto out;
+	}
+	
+	if (addr_size != sizeof(struct sockaddr_in)) {
+		async_answer_0(callid, EINVAL);
+		goto out;
+	}
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
 	rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
-	    addr, addr_len, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+	    addr, addr_size, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
 	    last_used_port);
 	if (rc != EOK) {
 		async_answer_0(callid, rc);
-		return;
-	}
-
+		goto out;
+	}
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
 	sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
-	if (sock_core != NULL) {
-		socket = (tcp_sockdata_t *)sock_core->specific_data;
-		/* XXX Anything to do? */
-		(void) socket;
-	}
-
+	if (sock_core == NULL) {
+		async_answer_0(callid, ENOENT);
+		goto out;
+	}
+	
+	socket = (tcp_sockdata_t *)sock_core->specific_data;
+	/* XXX Anything to do? */
+	(void) socket;
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - success");
 	async_answer_0(callid, EOK);
+	
+out:
+	if (addr != NULL)
+		free(addr);
 }
 
@@ -249,4 +264,5 @@
 	tcp_sock_lconn_t *lconn;
 	int i;
+	int rc;
 
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_listen()");
@@ -268,12 +284,24 @@
 		return;
 	}
-
-	socket = (tcp_sockdata_t *)sock_core->specific_data;
-
+	
+	if (sock_core->port <= 0) {
+		rc = socket_bind_free_port(&gsock, sock_core,
+		    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+		    last_used_port);
+		if (rc != EOK) {
+			async_answer_0(callid, rc);
+			return;
+		}
+		
+		last_used_port = sock_core->port;
+	}
+	
+	socket = (tcp_sockdata_t *) sock_core->specific_data;
+	
 	/*
 	 * Prepare @c backlog listening connections.
 	 */
 	fibril_mutex_lock(&socket->lock);
-
+	
 	socket->backlog = backlog;
 	socket->lconn = calloc(backlog, sizeof(tcp_conn_t *));
@@ -283,12 +311,12 @@
 		return;
 	}
-
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - open connections");
-
+	
 	lsocket.addr.ipv4 = TCP_IPV4_ANY;
 	lsocket.port = sock_core->port;
 	fsocket.addr.ipv4 = TCP_IPV4_ANY;
 	fsocket.port = TCP_PORT_ANY;
-
+	
 	for (i = 0; i < backlog; i++) {
 
@@ -362,5 +390,5 @@
 			return;
 		}
-
+		
 		last_used_port = sock_core->port;
 	}
@@ -441,4 +469,16 @@
 		return;
 	}
+	
+	if (sock_core->port <= 0) {
+		rc = socket_bind_free_port(&gsock, sock_core,
+		    TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
+		    last_used_port);
+		if (rc != EOK) {
+			async_answer_0(callid, rc);
+			return;
+		}
+		
+		last_used_port = sock_core->port;
+	}
 
 	socket = (tcp_sockdata_t *)sock_core->specific_data;
Index: uspace/srv/net/udp/assoc.c
===================================================================
--- uspace/srv/net/udp/assoc.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/srv/net/udp/assoc.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -200,6 +200,7 @@
 /** Set local socket in association.
  *
- * @param assoc		Association
- * @param fsock		Foreign socket (deeply copied)
+ * @param assoc Association
+ * @param lsock Local socket (deeply copied)
+ *
  */
 void udp_assoc_set_local(udp_assoc_t *assoc, udp_sock_t *lsock)
@@ -208,4 +209,18 @@
 	fibril_mutex_lock(&assoc->lock);
 	assoc->ident.local = *lsock;
+	fibril_mutex_unlock(&assoc->lock);
+}
+
+/** Set local port in association.
+ *
+ * @param assoc Association
+ * @param lport Local port
+ *
+ */
+void udp_assoc_set_local_port(udp_assoc_t *assoc, uint16_t lport)
+{
+	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_local(%p, %" PRIu16 ")", assoc, lport);
+	fibril_mutex_lock(&assoc->lock);
+	assoc->ident.local.port = lport;
 	fibril_mutex_unlock(&assoc->lock);
 }
@@ -406,5 +421,4 @@
 }
 
-
 /**
  * @}
Index: uspace/srv/net/udp/assoc.h
===================================================================
--- uspace/srv/net/udp/assoc.h	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/srv/net/udp/assoc.h	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -47,4 +47,5 @@
 extern void udp_assoc_set_foreign(udp_assoc_t *, udp_sock_t *);
 extern void udp_assoc_set_local(udp_assoc_t *, udp_sock_t *);
+extern void udp_assoc_set_local_port(udp_assoc_t *, uint16_t);
 extern int udp_assoc_send(udp_assoc_t *, udp_sock_t *, udp_msg_t *);
 extern int udp_assoc_recv(udp_assoc_t *, udp_msg_t **, udp_sock_t *);
Index: uspace/srv/net/udp/sock.c
===================================================================
--- uspace/srv/net/udp/sock.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/srv/net/udp/sock.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -90,5 +90,5 @@
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);
 	async_exch_t *exch = async_exchange_begin(sock_core->sess);
-	async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t)sock_core->socket_id,
+	async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) sock_core->socket_id,
 	    UDP_FRAGMENT_SIZE, 0, 0, 1);
 	async_exchange_end(exch);
@@ -177,5 +177,10 @@
 		goto out;
 	}
-
+	
+	if (addr_size != sizeof(struct sockaddr_in)) {
+		async_answer_0(callid, EINVAL);
+		goto out;
+	}
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind");
 	rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
@@ -186,10 +191,5 @@
 		goto out;
 	}
-
-	if (addr_size != sizeof(struct sockaddr_in)) {
-		async_answer_0(callid, EINVAL);
-		goto out;
-	}
-
+	
 	log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find");
 	sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call));
@@ -249,25 +249,13 @@
 static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
 {
-	int socket_id;
-	int fragments;
-	int index;
-	struct sockaddr_in *addr;
-	size_t addr_size;
-	socket_core_t *sock_core;
-	udp_sockdata_t *socket;
-	udp_sock_t fsock, *fsockp;
-	ipc_call_t answer;
-	ipc_callid_t wcallid;
-	size_t length;
-	uint8_t buffer[UDP_FRAGMENT_SIZE];
-	udp_error_t urc;
-	int rc;
-
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()");
-
-	addr = NULL;
-
+	
+	struct sockaddr_in *addr = NULL;
+	udp_sock_t fsock;
+	udp_sock_t *fsock_ptr;
+	
 	if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) {
-		rc = async_data_write_accept((void **) &addr, false,
+		size_t addr_size;
+		int rc = async_data_write_accept((void **) &addr, false,
 		    0, 0, 0, &addr_size);
 		if (rc != EOK) {
@@ -275,49 +263,63 @@
 			goto out;
 		}
-
+		
 		if (addr_size != sizeof(struct sockaddr_in)) {
 			async_answer_0(callid, EINVAL);
 			goto out;
 		}
-
+		
 		fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr);
 		fsock.port = uint16_t_be2host(addr->sin_port);
-		fsockp = &fsock;
-	} else {
-		fsockp = NULL;
-	}
-
-	socket_id = SOCKET_GET_SOCKET_ID(call);
-	fragments = SOCKET_GET_DATA_FRAGMENTS(call);
+		fsock_ptr = &fsock;
+	} else
+		fsock_ptr = NULL;
+	
+	int socket_id = SOCKET_GET_SOCKET_ID(call);
+	
 	SOCKET_GET_FLAGS(call);
-
-	sock_core = socket_cores_find(&client->sockets, socket_id);
+	
+	socket_core_t *sock_core =
+	    socket_cores_find(&client->sockets, socket_id);
 	if (sock_core == NULL) {
 		async_answer_0(callid, ENOTSOCK);
 		goto out;
 	}
-
-	if (sock_core->port == 0) {
+	
+	udp_sockdata_t *socket =
+	    (udp_sockdata_t *) sock_core->specific_data;
+	
+	if (sock_core->port <= 0) {
 		/* Implicitly bind socket to port */
-		rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call),
-		    addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
-		    last_used_port);
+		int rc = socket_bind_free_port(&gsock, sock_core,
+		    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, last_used_port);
 		if (rc != EOK) {
 			async_answer_0(callid, rc);
 			goto out;
 		}
-	}
-
-	socket = (udp_sockdata_t *)sock_core->specific_data;
+		
+		assert(sock_core->port > 0);
+		
+		udp_error_t urc = udp_uc_set_local_port(socket->assoc,
+		    sock_core->port);
+		
+		if (urc != UDP_EOK) {
+			// TODO: better error handling
+			async_answer_0(callid, EINTR);
+			goto out;
+		}
+		
+		last_used_port = sock_core->port;
+	}
+	
 	fibril_mutex_lock(&socket->lock);
-
+	
 	if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) {
 		/* Determine local IP address */
 		inet_addr_t loc_addr, rem_addr;
-
-		rem_addr.ipv4 = fsockp ? fsock.addr.ipv4 :
+		
+		rem_addr.ipv4 = fsock_ptr ? fsock.addr.ipv4 :
 		    socket->assoc->ident.foreign.addr.ipv4;
-
-		rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
+		
+		int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);
 		if (rc != EOK) {
 			fibril_mutex_unlock(&socket->lock);
@@ -327,14 +329,17 @@
 			return;
 		}
-
+		
 		socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4;
 		log_msg(LOG_DEFAULT, LVL_DEBUG, "Local IP address is %x",
 		    socket->assoc->ident.local.addr.ipv4);
 	}
-
-
+	
 	assert(socket->assoc != NULL);
-
-	for (index = 0; index < fragments; index++) {
+	
+	int fragments = SOCKET_GET_DATA_FRAGMENTS(call);
+	for (int index = 0; index < fragments; index++) {
+		ipc_callid_t wcallid;
+		size_t length;
+		
 		if (!async_data_write_receive(&wcallid, &length)) {
 			fibril_mutex_unlock(&socket->lock);
@@ -342,9 +347,10 @@
 			goto out;
 		}
-
+		
 		if (length > UDP_FRAGMENT_SIZE)
 			length = UDP_FRAGMENT_SIZE;
-
-		rc = async_data_write_finalize(wcallid, buffer, length);
+		
+		uint8_t buffer[UDP_FRAGMENT_SIZE];
+		int rc = async_data_write_finalize(wcallid, buffer, length);
 		if (rc != EOK) {
 			fibril_mutex_unlock(&socket->lock);
@@ -352,24 +358,25 @@
 			goto out;
 		}
-
-		urc = udp_uc_send(socket->assoc, fsockp, buffer, length, 0);
-
+		
+		udp_error_t urc =
+		    udp_uc_send(socket->assoc, fsock_ptr, buffer, length, 0);
+		
 		switch (urc) {
 		case UDP_EOK:
 			rc = EOK;
 			break;
-/*		case TCP_ENOTEXIST:
-			rc = ENOTCONN;
-			break;
-		case TCP_ECLOSING:
-			rc = ENOTCONN;
-			break;
-		case TCP_ERESET:
-			rc = ECONNABORTED;
-			break;*/
+		case UDP_ENORES:
+			rc = ENOMEM;
+			break;
+		case UDP_EUNSPEC:
+			rc = EINVAL;
+			break;
+		case UDP_ENOROUTE:
+			rc = EIO;
+			break;
 		default:
 			assert(false);
 		}
-
+		
 		if (rc != EOK) {
 			fibril_mutex_unlock(&socket->lock);
@@ -378,4 +385,6 @@
 		}
 	}
+	
+	ipc_call_t answer;
 	
 	IPC_SET_ARG1(answer, 0);
@@ -526,22 +535,19 @@
 static void udp_sock_close(udp_client_t *client, ipc_callid_t callid, ipc_call_t call)
 {
-	int socket_id;
-	socket_core_t *sock_core;
-	udp_sockdata_t *socket;
-	int rc;
-
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()");
-	socket_id = SOCKET_GET_SOCKET_ID(call);
-
-	sock_core = socket_cores_find(&client->sockets, socket_id);
+	int socket_id = SOCKET_GET_SOCKET_ID(call);
+	
+	socket_core_t *sock_core =
+	    socket_cores_find(&client->sockets, socket_id);
 	if (sock_core == NULL) {
 		async_answer_0(callid, ENOTSOCK);
 		return;
 	}
-
-	socket = (udp_sockdata_t *)sock_core->specific_data;
+	
+	udp_sockdata_t *socket =
+	    (udp_sockdata_t *) sock_core->specific_data;
 	fibril_mutex_lock(&socket->lock);
-
-	rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
+	
+	int rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,
 	    udp_free_sock_data);
 	if (rc != EOK) {
@@ -550,5 +556,5 @@
 		return;
 	}
-
+	
 	fibril_mutex_unlock(&socket->lock);
 	async_answer_0(callid, EOK);
@@ -583,12 +589,12 @@
 			    &sock->recv_buffer_lock);
 		}
-
+		
 		log_msg(LOG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()");
 		urc = udp_uc_receive(sock->assoc, sock->recv_buffer,
 		    UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock);
 		sock->recv_error = urc;
-
+		
 		udp_sock_notify_data(sock->sock_core);
-
+		
 		if (urc != UDP_EOK) {
 			fibril_condvar_broadcast(&sock->recv_buffer_cv);
@@ -596,7 +602,7 @@
 			break;
 		}
-
+		
 		log_msg(LOG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");
-
+		
 		sock->recv_buffer_used = rcvd;
 		fibril_mutex_unlock(&sock->recv_buffer_lock);
Index: uspace/srv/net/udp/ucall.c
===================================================================
--- uspace/srv/net/udp/ucall.c	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/srv/net/udp/ucall.c	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -68,6 +68,14 @@
 {
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_uc_set_local(%p, %p)", assoc, lsock);
+	
+	udp_assoc_set_local(assoc, lsock);
+	return UDP_EOK;
+}
 
-	udp_assoc_set_local(assoc, lsock);
+udp_error_t udp_uc_set_local_port(udp_assoc_t *assoc, uint16_t lport)
+{
+	log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_uc_set_local(%p, %" PRIu16 ")", assoc, lport);
+	
+	udp_assoc_set_local_port(assoc, lport);
 	return UDP_EOK;
 }
Index: uspace/srv/net/udp/ucall.h
===================================================================
--- uspace/srv/net/udp/ucall.h	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/srv/net/udp/ucall.h	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -42,4 +42,5 @@
 extern udp_error_t udp_uc_set_foreign(udp_assoc_t *, udp_sock_t *);
 extern udp_error_t udp_uc_set_local(udp_assoc_t *, udp_sock_t *);
+extern udp_error_t udp_uc_set_local_port(udp_assoc_t *, uint16_t);
 extern udp_error_t udp_uc_send(udp_assoc_t *, udp_sock_t *, void *, size_t,
     xflags_t);
Index: uspace/srv/net/udp/udp_type.h
===================================================================
--- uspace/srv/net/udp/udp_type.h	(revision b696cbff4a24376d6e6be1b86817a2cf94bab1e3)
+++ uspace/srv/net/udp/udp_type.h	(revision 0dfa93b0cda81f93598fc30620d75a22c14f06d2)
@@ -66,5 +66,5 @@
 };
 
-enum tcp_port {
+enum udp_port {
 	UDP_PORT_ANY = 0
 };
