Index: uspace/lib/c/generic/net/socket_client.c
===================================================================
--- uspace/lib/c/generic/net/socket_client.c	(revision 1bfd3d3b2991b8f2df4cff87d9723059528b9bd1)
+++ uspace/lib/c/generic/net/socket_client.c	(revision b35fea35ac7e82f4ee646caa0e340f694339fb64)
@@ -214,5 +214,5 @@
 	callid = async_get_call(&call);
 
-	switch (IPC_GET_METHOD(call)) {
+	switch (IPC_GET_IMETHOD(call)) {
 	case NET_SOCKET_RECEIVED:
 	case NET_SOCKET_ACCEPTED:
@@ -220,5 +220,5 @@
 		fibril_rwlock_read_lock(&socket_globals.lock);
 
-		// find the socket
+		/* Find the socket */
 		socket = sockets_find(socket_get_sockets(),
 		    SOCKET_GET_SOCKET_ID(call));
@@ -229,13 +229,13 @@
 		}
 		
-		switch (IPC_GET_METHOD(call)) {
+		switch (IPC_GET_IMETHOD(call)) {
 		case NET_SOCKET_RECEIVED:
 			fibril_mutex_lock(&socket->receive_lock);
-			// push the number of received packet fragments
+			/* 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
+				/* Signal the received packet */
 				fibril_condvar_signal(&socket->receive_signal);
 			}
@@ -244,10 +244,10 @@
 
 		case NET_SOCKET_ACCEPTED:
-			// push the new socket identifier
+			/* 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
+				/* Signal the accepted socket */
 				fibril_condvar_signal(&socket->accept_signal);
 			}
@@ -264,5 +264,5 @@
 			fibril_rwlock_write_lock(&socket->sending_lock);
 
-			// set the data fragment size
+			/* Set the data fragment size */
 			socket->data_fragment_size =
 			    SOCKET_GET_DATA_FRAGMENT_SIZE(call);
@@ -278,5 +278,5 @@
 	}
 
-	ipc_answer_0(callid, (ipcarg_t) rc);
+	ipc_answer_0(callid, (sysarg_t) rc);
 	goto loop;
 }
@@ -342,5 +342,5 @@
 			socket_id = 1;
 			++count;
-		// only this branch for last_id
+		/* Only this branch for last_id */
 		} else {
 			if (socket_id < INT_MAX) {
@@ -404,9 +404,9 @@
 	int socket_id;
 	services_t service;
-	ipcarg_t fragment_size;
-	ipcarg_t header_size;
+	sysarg_t fragment_size;
+	sysarg_t header_size;
 	int rc;
 
-	// find the appropriate service
+	/* Find the appropriate service */
 	switch (domain) {
 	case PF_INET:
@@ -457,5 +457,5 @@
 		return phone;
 
-	// create a new socket structure
+	/* Create a new socket structure */
 	socket = (socket_t *) malloc(sizeof(socket_t));
 	if (!socket)
@@ -465,5 +465,5 @@
 	fibril_rwlock_write_lock(&socket_globals.lock);
 
-	// request a new socket
+	/* Request a new socket */
 	socket_id = socket_generate_new_id();
 	if (socket_id <= 0) {
@@ -484,7 +484,7 @@
 	socket->header_size = (size_t) header_size;
 
-	// finish the new socket initialization
+	/* Finish the new socket initialization */
 	socket_initialize(socket, socket_id, phone, service);
-	// store the new socket
+	/* Store the new socket */
 	rc = sockets_add(socket_get_sockets(), socket_id, socket);
 
@@ -494,5 +494,5 @@
 		dyn_fifo_destroy(&socket->accepted);
 		free(socket);
-		async_msg_3(phone, NET_SOCKET_CLOSE, (ipcarg_t) socket_id, 0,
+		async_msg_3(phone, NET_SOCKET_CLOSE, (sysarg_t) socket_id, 0,
 		    service);
 		return rc;
@@ -516,10 +516,10 @@
  */
 static int
-socket_send_data(int socket_id, ipcarg_t message, ipcarg_t arg2,
+socket_send_data(int socket_id, sysarg_t message, sysarg_t arg2,
     const void *data, size_t datalength)
 {
 	socket_t *socket;
 	aid_t message_id;
-	ipcarg_t result;
+	sysarg_t result;
 
 	if (!data)
@@ -531,5 +531,5 @@
 	fibril_rwlock_read_lock(&socket_globals.lock);
 
-	// find the socket
+	/* Find the socket */
 	socket = sockets_find(socket_get_sockets(), socket_id);
 	if (!socket) {
@@ -538,8 +538,8 @@
 	}
 
-	// request the message
+	/* Request the message */
 	message_id = async_send_3(socket->phone, message,
-	    (ipcarg_t) socket->socket_id, arg2, socket->service, NULL);
-	// send the address
+	    (sysarg_t) socket->socket_id, arg2, socket->service, NULL);
+	/* Send the address */
 	async_data_write_start(socket->phone, data, datalength);
 
@@ -566,5 +566,5 @@
 		return EINVAL;
 
-	// send the address
+	/* Send the address */
 	return socket_send_data(socket_id, NET_SOCKET_BIND, 0, my_addr,
 	    (size_t) addrlen);
@@ -591,5 +591,5 @@
 	fibril_rwlock_read_lock(&socket_globals.lock);
 
-	// find the socket
+	/* Find the socket */
 	socket = sockets_find(socket_get_sockets(), socket_id);
 	if (!socket) {
@@ -598,7 +598,7 @@
 	}
 
-	// request listen backlog change
+	/* Request listen backlog change */
 	result = (int) async_req_3_0(socket->phone, NET_SOCKET_LISTEN,
-	    (ipcarg_t) socket->socket_id, (ipcarg_t) backlog, socket->service);
+	    (sysarg_t) socket->socket_id, (sysarg_t) backlog, socket->service);
 
 	fibril_rwlock_read_unlock(&socket_globals.lock);
@@ -625,5 +625,5 @@
 	socket_t *new_socket;
 	aid_t message_id;
-	ipcarg_t ipc_result;
+	sysarg_t ipc_result;
 	int result;
 	ipc_call_t answer;
@@ -634,5 +634,5 @@
 	fibril_rwlock_write_lock(&socket_globals.lock);
 
-	// find the socket
+	/* Find the socket */
 	socket = sockets_find(socket_get_sockets(), socket_id);
 	if (!socket) {
@@ -643,10 +643,10 @@
 	fibril_mutex_lock(&socket->accept_lock);
 
-	// wait for an accepted socket
+	/* Wait for an accepted socket */
 	++ socket->blocked;
 	while (dyn_fifo_value(&socket->accepted) <= 0) {
 		fibril_rwlock_write_unlock(&socket_globals.lock);
 		fibril_condvar_wait(&socket->accept_signal, &socket->accept_lock);
-		// drop the accept lock to avoid deadlock
+		/* Drop the accept lock to avoid deadlock */
 		fibril_mutex_unlock(&socket->accept_lock);
 		fibril_rwlock_write_lock(&socket_globals.lock);
@@ -655,5 +655,5 @@
 	-- socket->blocked;
 
-	// create a new scoket
+	/* Create a new socket */
 	new_socket = (socket_t *) malloc(sizeof(socket_t));
 	if (!new_socket) {
@@ -681,10 +681,10 @@
 	}
 
-	// request accept
+	/* Request accept */
 	message_id = async_send_5(socket->phone, NET_SOCKET_ACCEPT,
-	    (ipcarg_t) socket->socket_id, 0, socket->service, 0,
+	    (sysarg_t) socket->socket_id, 0, socket->service, 0,
 	    new_socket->socket_id, &answer);
 
-	// read address
+	/* Read address */
 	ipc_data_read_start(socket->phone, cliaddr, *addrlen);
 	fibril_rwlock_write_unlock(&socket_globals.lock);
@@ -695,12 +695,12 @@
 			result = EINVAL;
 
-		// dequeue the accepted socket if successful
+		/* Dequeue the accepted socket if successful */
 		dyn_fifo_pop(&socket->accepted);
-		// set address length
+		/* Set address length */
 		*addrlen = SOCKET_GET_ADDRESS_LENGTH(answer);
 		new_socket->data_fragment_size =
 		    SOCKET_GET_DATA_FRAGMENT_SIZE(answer);
 	} else if (result == ENOTSOCK) {
-		// empty the queue if no accepted sockets
+		/* Empty the queue if no accepted sockets */
 		while (dyn_fifo_pop(&socket->accepted) > 0)
 			;
@@ -731,5 +731,5 @@
 		return EDESTADDRREQ;
 
-	// send the address
+	/* Send the address */
 	return socket_send_data(socket_id, NET_SOCKET_CONNECT, 0, serv_addr,
 	    addrlen);
@@ -744,5 +744,5 @@
 	int accepted_id;
 
-	// destroy all accepted sockets
+	/* Destroy all accepted sockets */
 	while ((accepted_id = dyn_fifo_pop(&socket->accepted)) >= 0)
 		socket_destroy(sockets_find(socket_get_sockets(), accepted_id));
@@ -780,12 +780,12 @@
 	}
 
-	// request close
+	/* Request close */
 	rc = (int) async_req_3_0(socket->phone, NET_SOCKET_CLOSE,
-	    (ipcarg_t) socket->socket_id, 0, socket->service);
+	    (sysarg_t) socket->socket_id, 0, socket->service);
 	if (rc != EOK) {
 		fibril_rwlock_write_unlock(&socket_globals.lock);
 		return rc;
 	}
-	// free the socket structure
+	/* Free the socket structure */
 	socket_destroy(socket);
 
@@ -815,5 +815,5 @@
  */
 static int
-sendto_core(ipcarg_t message, int socket_id, const void *data,
+sendto_core(sysarg_t message, int socket_id, const void *data,
     size_t datalength, int flags, const struct sockaddr *toaddr,
     socklen_t addrlen)
@@ -821,5 +821,5 @@
 	socket_t *socket;
 	aid_t message_id;
-	ipcarg_t result;
+	sysarg_t result;
 	size_t fragments;
 	ipc_call_t answer;
@@ -833,5 +833,5 @@
 	fibril_rwlock_read_lock(&socket_globals.lock);
 
-	// find socket
+	/* Find socket */
 	socket = sockets_find(socket_get_sockets(), socket_id);
 	if (!socket) {
@@ -842,5 +842,5 @@
 	fibril_rwlock_read_lock(&socket->sending_lock);
 
-	// compute data fragment count
+	/* Compute data fragment count */
 	if (socket->data_fragment_size > 0) {
 		fragments = (datalength + socket->header_size) /
@@ -853,18 +853,18 @@
 	}
 
-	// request send
+	/* Request send */
 	message_id = async_send_5(socket->phone, message,
-	    (ipcarg_t) socket->socket_id,
+	    (sysarg_t) socket->socket_id,
 	    (fragments == 1 ? datalength : socket->data_fragment_size),
-	    socket->service, (ipcarg_t) flags, fragments, &answer);
-
-	// send the address if given
+	    socket->service, (sysarg_t) flags, fragments, &answer);
+
+	/* Send the address if given */
 	if (!toaddr ||
 	    (async_data_write_start(socket->phone, toaddr, addrlen) == EOK)) {
 		if (fragments == 1) {
-			// send all if only one fragment
+			/* Send all if only one fragment */
 			async_data_write_start(socket->phone, data, datalength);
 		} else {
-			// send the first fragment
+			/* Send the first fragment */
 			async_data_write_start(socket->phone, data,
 			    socket->data_fragment_size - socket->header_size);
@@ -872,5 +872,5 @@
 			    socket->data_fragment_size - socket->header_size;
 	
-			// send the middle fragments
+			/* Send the middle fragments */
 			while (--fragments > 1) {
 				async_data_write_start(socket->phone, data,
@@ -880,5 +880,5 @@
 			}
 
-			// send the last fragment
+			/* Send the last fragment */
 			async_data_write_start(socket->phone, data,
 			    (datalength + socket->header_size) %
@@ -892,5 +892,5 @@
 	    (SOCKET_GET_DATA_FRAGMENT_SIZE(answer) !=
 	    socket->data_fragment_size)) {
-		// set the data fragment size
+		/* Set the data fragment size */
 		socket->data_fragment_size =
 		    SOCKET_GET_DATA_FRAGMENT_SIZE(answer);
@@ -917,5 +917,5 @@
 int send(int socket_id, void *data, size_t datalength, int flags)
 {
-	// without the address
+	/* Without the address */
 	return sendto_core(NET_SOCKET_SEND, socket_id, data, datalength, flags,
 	    NULL, 0);
@@ -950,5 +950,5 @@
 		return EDESTADDRREQ;
 
-	// with the address
+	/* With the address */
 	return sendto_core(NET_SOCKET_SENDTO, socket_id, data, datalength,
 	    flags, toaddr, addrlen);
@@ -966,5 +966,6 @@
  *			read. The actual address length is set. Used only if
  *			fromaddr is not NULL.
- * @return		EOK on success.
+ * @return		Positive received message size in bytes on success.
+ * @return		Zero if no more data (other side closed the connection).
  * @return		ENOTSOCK if the socket is not found.
  * @return		EBADMEM if the data parameter is NULL.
@@ -972,11 +973,11 @@
  * @return		Other error codes as defined for the spcific message.
  */
-static int
-recvfrom_core(ipcarg_t message, int socket_id, void *data, size_t datalength,
+static ssize_t
+recvfrom_core(sysarg_t message, int socket_id, void *data, size_t datalength,
     int flags, struct sockaddr *fromaddr, socklen_t *addrlen)
 {
 	socket_t *socket;
 	aid_t message_id;
-	ipcarg_t ipc_result;
+	sysarg_t ipc_result;
 	int result;
 	size_t fragments;
@@ -984,4 +985,5 @@
 	size_t index;
 	ipc_call_t answer;
+	ssize_t retval;
 
 	if (!data)
@@ -996,5 +998,5 @@
 	fibril_rwlock_read_lock(&socket_globals.lock);
 
-	// find the socket
+	/* Find the socket */
 	socket = sockets_find(socket_get_sockets(), socket_id);
 	if (!socket) {
@@ -1004,12 +1006,12 @@
 
 	fibril_mutex_lock(&socket->receive_lock);
-	// wait for a received packet
+	/* Wait for a received packet */
 	++socket->blocked;
-	while ((result = dyn_fifo_value(&socket->received)) <= 0) {
+	while ((result = dyn_fifo_value(&socket->received)) < 0) {
 		fibril_rwlock_read_unlock(&socket_globals.lock);
 		fibril_condvar_wait(&socket->receive_signal,
 		    &socket->receive_lock);
 
-		// drop the receive lock to avoid deadlock
+		/* Drop the receive lock to avoid deadlock */
 		fibril_mutex_unlock(&socket->receive_lock);
 		fibril_rwlock_read_lock(&socket_globals.lock);
@@ -1019,5 +1021,12 @@
 	fragments = (size_t) result;
 
-	// prepare lengths if more fragments
+	if (fragments == 0) {
+		/* No more data, other side has closed the connection. */
+		fibril_mutex_unlock(&socket->receive_lock);
+		fibril_rwlock_read_unlock(&socket_globals.lock);
+		return 0;
+	}
+
+	/* Prepare lengths if more fragments */
 	if (fragments > 1) {
 		lengths = (size_t *) malloc(sizeof(size_t) * fragments +
@@ -1029,19 +1038,19 @@
 		}
 
-		// request packet data
+		/* Request packet data */
 		message_id = async_send_4(socket->phone, message,
-		    (ipcarg_t) socket->socket_id, 0, socket->service,
-		    (ipcarg_t) flags, &answer);
-
-		// read the address if desired
+		    (sysarg_t) socket->socket_id, 0, socket->service,
+		    (sysarg_t) flags, &answer);
+
+		/* Read the address if desired */
 		if(!fromaddr ||
 		    (async_data_read_start(socket->phone, fromaddr,
 		    *addrlen) == EOK)) {
-			// read the fragment lengths
+			/* Read the fragment lengths */
 			if (async_data_read_start(socket->phone, lengths,
 			    sizeof(int) * (fragments + 1)) == EOK) {
 				if (lengths[fragments] <= datalength) {
 
-					// read all fragments if long enough
+					/* Read all fragments if long enough */
 					for (index = 0; index < fragments;
 					    ++index) {
@@ -1057,15 +1066,15 @@
 
 		free(lengths);
-	} else {
-		// request packet data
+	} else { /* fragments == 1 */
+		/* Request packet data */
 		message_id = async_send_4(socket->phone, message,
-		    (ipcarg_t) socket->socket_id, 0, socket->service,
-		    (ipcarg_t) flags, &answer);
-
-		// read the address if desired
+		    (sysarg_t) socket->socket_id, 0, socket->service,
+		    (sysarg_t) flags, &answer);
+
+		/* Read the address if desired */
 		if (!fromaddr ||
 		    (async_data_read_start(socket->phone, fromaddr,
 		        *addrlen) == EOK)) {
-			// read all if only one fragment
+			/* Read all if only one fragment */
 			async_data_read_start(socket->phone, data, datalength);
 		}
@@ -1075,16 +1084,18 @@
 	result = (int) ipc_result;
 	if (result == EOK) {
-		// dequeue the received packet
+		/* Dequeue the received packet */
 		dyn_fifo_pop(&socket->received);
-		// return read data length
-		result = SOCKET_GET_READ_DATA_LENGTH(answer);
-		// set address length
+		/* Return read data length */
+		retval = SOCKET_GET_READ_DATA_LENGTH(answer);
+		/* Set address length */
 		if (fromaddr && addrlen)
 			*addrlen = SOCKET_GET_ADDRESS_LENGTH(answer);
+	} else {
+		retval = (ssize_t) result;
 	}
 
 	fibril_mutex_unlock(&socket->receive_lock);
 	fibril_rwlock_read_unlock(&socket_globals.lock);
-	return result;
+	return retval;
 }
 
@@ -1095,5 +1106,6 @@
  * @param[in] datalength The data length.
  * @param[in] flags	Various receive flags.
- * @return		EOK on success.
+ * @return		Positive received message size in bytes on success.
+ * @return		Zero if no more data (other side closed the connection).
  * @return		ENOTSOCK if the socket is not found.
  * @return		EBADMEM if the data parameter is NULL.
@@ -1102,7 +1114,7 @@
  *			message.
  */
-int recv(int socket_id, void *data, size_t datalength, int flags)
-{
-	// without the address
+ssize_t recv(int socket_id, void *data, size_t datalength, int flags)
+{
+	/* Without the address */
 	return recvfrom_core(NET_SOCKET_RECV, socket_id, data, datalength,
 	    flags, NULL, NULL);
@@ -1118,5 +1130,6 @@
  * @param[in,out] addrlen The address length. The maximum address length is
  *			read. The actual address length is set.
- * @return		EOK on success.
+ * @return		Positive received message size in bytes on success.
+ * @return		Zero if no more data (other side closed the connection).
  * @return		ENOTSOCK if the socket is not found.
  * @return		EBADMEM if the data or fromaddr parameter is NULL.
@@ -1125,5 +1138,5 @@
  *			message.
  */
-int
+ssize_t
 recvfrom(int socket_id, void *data, size_t datalength, int flags,
     struct sockaddr *fromaddr, socklen_t *addrlen)
@@ -1135,5 +1148,5 @@
 		return NO_DATA;
 
-	// with the address
+	/* With the address */
 	return recvfrom_core(NET_SOCKET_RECVFROM, socket_id, data, datalength,
 	    flags, fromaddr, addrlen);
@@ -1160,5 +1173,5 @@
 	socket_t *socket;
 	aid_t message_id;
-	ipcarg_t result;
+	sysarg_t result;
 
 	if (!value || !optlen)
@@ -1170,5 +1183,5 @@
 	fibril_rwlock_read_lock(&socket_globals.lock);
 
-	// find the socket
+	/* Find the socket */
 	socket = sockets_find(socket_get_sockets(), socket_id);
 	if (!socket) {
@@ -1177,13 +1190,13 @@
 	}
 
-	// request option value
+	/* Request option value */
 	message_id = async_send_3(socket->phone, NET_SOCKET_GETSOCKOPT,
-	    (ipcarg_t) socket->socket_id, (ipcarg_t) optname, socket->service,
+	    (sysarg_t) socket->socket_id, (sysarg_t) optname, socket->service,
 	    NULL);
 
-	// read the length
+	/* Read the length */
 	if (async_data_read_start(socket->phone, optlen,
 	    sizeof(*optlen)) == EOK) {
-		// read the value
+		/* Read the value */
 		async_data_read_start(socket->phone, value, *optlen);
 	}
@@ -1212,7 +1225,7 @@
     size_t optlen)
 {
-	// send the value
+	/* Send the value */
 	return socket_send_data(socket_id, NET_SOCKET_SETSOCKOPT,
-	    (ipcarg_t) optname, value, optlen);
+	    (sysarg_t) optname, value, optlen);
 }
 
