Index: uspace/srv/hid/remcons/remcons.c
===================================================================
--- uspace/srv/hid/remcons/remcons.c	(revision 21a9869dd7ae0958082513a5b3a141997c21a20a)
+++ uspace/srv/hid/remcons/remcons.c	(revision 5576358558520519ae33f1f8bb40aeee21bbd77f)
@@ -67,4 +67,5 @@
 	int socket;
 	service_id_t service_id;
+	char *service_name;
 	/** Producer-consumer of kbd_event_t. */
 	prodcons_t in_events;
@@ -75,6 +76,67 @@
 } client_t;
 
+static FIBRIL_MUTEX_INITIALIZE(clients_guard);
 static LIST_INITIALIZE(clients);
-static int client_counter = 0;
+
+static client_t *client_create(int socket)
+{
+	static int client_counter = 0;
+
+	client_t *client = malloc(sizeof(client_t));
+	if (client == NULL) {
+		return NULL;
+	}
+
+	client->id = ++client_counter;
+
+	int rc = asprintf(&client->service_name, "%s/telnet%d", NAMESPACE, client->id);
+	if (rc < 0) {
+		free(client);
+		return NULL;
+	}
+
+	client->socket = socket;
+	client->service_id = (service_id_t) -1;
+	prodcons_initialize(&client->in_events);
+	link_initialize(&client->link);
+	client->socket_buffer_len = 0;
+	client->socket_buffer_pos = 0;
+
+
+	fibril_mutex_lock(&clients_guard);
+	list_append(&client->link, &clients);
+	fibril_mutex_unlock(&clients_guard);
+
+	return client;
+}
+
+static void client_destroy(client_t *client)
+{
+	assert(client);
+
+	fibril_mutex_lock(&clients_guard);
+	list_remove(&client->link);
+	fibril_mutex_unlock(&clients_guard);
+
+	free(client);
+}
+
+static client_t *client_find(service_id_t id)
+{
+	client_t *client = NULL;
+
+	fibril_mutex_lock(&clients_guard);
+	list_foreach(clients, link) {
+		client_t *tmp = list_get_instance(link, client_t, link);
+		if (tmp->service_id == id) {
+			client = tmp;
+			break;
+		}
+	}
+	fibril_mutex_unlock(&clients_guard);
+
+	return client;
+}
+
 
 
@@ -94,16 +156,6 @@
 static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
 {
-	service_id_t id = IPC_GET_ARG1(*icall);
-
 	/* Find the client. */
-	client_t *client = NULL;
-	list_foreach(clients, link) {
-		client_t *tmp = list_get_instance(link, client_t, link);
-		if (tmp->service_id == IPC_GET_ARG1(*icall)) {
-			client = tmp;
-			break;
-		}
-	}
-
+	client_t *client = client_find(IPC_GET_ARG1(*icall));
 	if (client == NULL) {
 		async_answer_0(iid, ENOENT);
@@ -111,5 +163,5 @@
 	}
 
-	printf("New client for service %" PRIun ".\n", id);
+	printf("New client for service %s.\n", client->service_name);
 
 	/* Accept the connection */
@@ -256,22 +308,15 @@
 	client_t *client = arg;
 
-	// FIXME: locking
-	list_append(&client->link, &clients);
-
-	char vc[LOC_NAME_MAXLEN + 1];
-	snprintf(vc, LOC_NAME_MAXLEN, "%s/rem%d", NAMESPACE, client->id);
-	if (loc_service_register(vc, &client->service_id) != EOK) {
-		fprintf(stderr, "%s: Unable to register device %s\n", NAME, vc);
+	rc = loc_service_register(client->service_name, &client->service_id);
+	if (rc != EOK) {
+		fprintf(stderr, "%s: Unable to register device %s\n", NAME,
+		    client->service_name);
 		return EOK;
 	}
-	printf("Service %s registered as %" PRIun "\n", vc, client->service_id);
-
-
-	prodcons_initialize(&client->in_events);
-	client->socket_buffer_len = 0;
-	client->socket_buffer_pos = 0;
+	printf("Service %s registered as %" PRIun "\n", client->service_name,
+	    client->service_id);
 
 	char term[LOC_NAME_MAXLEN];
-	snprintf(term, LOC_NAME_MAXLEN, "%s/%s", "/loc", vc);
+	snprintf(term, LOC_NAME_MAXLEN, "%s/%s", "/loc", client->service_name);
 
 	task_id_t task;
@@ -289,4 +334,6 @@
 
 	closesocket(client->socket);
+
+	client_destroy(client);
 
 	return EOK;
@@ -350,8 +397,6 @@
 		}
 
-		client_t *client = malloc(sizeof(client_t));
+		client_t *client = client_create(conn_sd);
 		assert(client);
-		client->id = ++client_counter;
-		client->socket = conn_sd;
 
 		fid_t fid = fibril_create(network_client_fibril, client);
