Index: uspace/lib/c/generic/async/client.c
===================================================================
--- uspace/lib/c/generic/async/client.c	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/async/client.c	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -124,7 +124,4 @@
 static fibril_rmutex_t message_mutex;
 
-/** Primary session (spawn parent, later naming service) */
-async_sess_t *session_primary = NULL;
-
 /** Message data */
 typedef struct {
@@ -169,16 +166,21 @@
 
 
-static async_sess_t *create_session_primary(void)
+/** Create session for existing phone
+ *
+ * @return session on success, NULL on error
+ */
+ 
+async_sess_t *create_session(cap_phone_handle_t phone, exch_mgmt_t mgmt,
+    sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
 {
 	async_sess_t *session = (async_sess_t *) malloc(sizeof(async_sess_t));
 
 	if (session != NULL) {
-		// TODO extract common part with async_connect_me_to
-		session_ns->iface = 0;
-		session->mgmt = EXCHANGE_ATOMIC;
-		session->phone = PHONE_INITIAL;
-		session->arg1 = 0;
-		session->arg2 = 0;
-		session->arg3 = 0;
+		session->iface = 0;
+		session->mgmt = mgmt;
+		session->phone = phone;
+		session->arg1 = arg1;
+		session->arg2 = arg2;
+		session->arg3 = arg3;
 		
 		fibril_mutex_initialize(&session->remote_state_mtx);
@@ -189,4 +191,6 @@
 		atomic_set(&session->refcnt, 0);
 		&session.exchanges = 0;
+	} else {
+		errno = ENOMEM;
 	}
 
@@ -196,19 +200,9 @@
 
 /** Initialize the async framework.
- * @param arg_session_primary Primary session (to naming service).
- *
- */
-void __async_client_init(async_sess_t *arg_session_primary)
+ *
+ */
+void __async_client_init(void)
 {
 	if (fibril_rmutex_initialize(&message_mutex) != EOK)
-		abort();
-
-	if (arg_session_primary == NULL) {
-		session_primary = create_session_primary();
-	} else {
-		session_primary = arg_session_primary;
-	}
-
-	if (session_primary == NULL)
 		abort();
 }
@@ -832,19 +826,4 @@
 }
 
-/** Injects another session instead of original primary session
- *
- * @param  session  Session to naming service.
- *
- * @return old primary session (to spawn parent)
- */
-async_sess_t *async_session_primary_swap(async_sess_t *session)
-{
-	assert(session_primary->phone == PHONE_INITIAL);
-
-	async_sess_t *old_primary = session_primary;
-	session_primary = session;
-	return old_primary;
-}
-
 /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
  *
@@ -865,10 +844,4 @@
 	if (exch == NULL) {
 		errno = ENOENT;
-		return NULL;
-	}
-
-	async_sess_t *sess = calloc(1, sizeof(async_sess_t));
-	if (sess == NULL) {
-		errno = ENOMEM;
 		return NULL;
 	}
@@ -879,17 +852,11 @@
 	if (rc != EOK) {
 		errno = rc;
-		free(sess);
 		return NULL;
 	}
 
-	sess->iface = iface;
-	sess->phone = phone;
-	sess->arg1 = iface;
-	sess->arg2 = arg2;
-	sess->arg3 = arg3;
-
-	fibril_mutex_initialize(&sess->remote_state_mtx);
-	list_initialize(&sess->exch_list);
-	fibril_mutex_initialize(&sess->mutex);
+	async_sess_t *sess = create_session(phone, mgmt, iface, arg2, arg3);
+	if (sess == NULL) {
+		ipc_hangup(phone);
+	}
 
 	return sess;
@@ -933,10 +900,4 @@
 	if (exch == NULL) {
 		errno = ENOENT;
-		return NULL;
-	}
-
-	async_sess_t *sess = calloc(1, sizeof(async_sess_t));
-	if (sess == NULL) {
-		errno = ENOMEM;
 		return NULL;
 	}
@@ -951,13 +912,8 @@
 	}
 
-	sess->iface = iface;
-	sess->phone = phone;
-	sess->arg1 = iface;
-	sess->arg2 = arg2;
-	sess->arg3 = arg3;
-
-	fibril_mutex_initialize(&sess->remote_state_mtx);
-	list_initialize(&sess->exch_list);
-	fibril_mutex_initialize(&sess->mutex);
+	async_sess_t *sess = create_session(phone, mgmt, iface, arg2, arg3);
+	if (sess == NULL) {
+		ipc_hangup(phone);
+	}
 
 	return sess;
@@ -969,25 +925,15 @@
 async_sess_t *async_connect_kbox(task_id_t id)
 {
-	async_sess_t *sess = calloc(1, sizeof(async_sess_t));
-	if (sess == NULL) {
-		errno = ENOMEM;
-		return NULL;
-	}
-
 	cap_phone_handle_t phone;
 	errno_t rc = ipc_connect_kbox(id, &phone);
 	if (rc != EOK) {
 		errno = rc;
-		free(sess);
 		return NULL;
 	}
 
-	sess->iface = 0;
-	sess->mgmt = EXCHANGE_ATOMIC;
-	sess->phone = phone;
-
-	fibril_mutex_initialize(&sess->remote_state_mtx);
-	list_initialize(&sess->exch_list);
-	fibril_mutex_initialize(&sess->mutex);
+	async_sess_t *sess = create_session(phone, EXCHANGE_ATOMIC, 0, 0, 0);
+	if (sess == NULL) {
+		ipc_hangup(phone);
+	}
 
 	return sess;
Index: uspace/lib/c/generic/async/server.c
===================================================================
--- uspace/lib/c/generic/async/server.c	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/async/server.c	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -391,8 +391,9 @@
  *                    calls routed through that phone.
  * @param in_task_id  Identification of the incoming connection.
- * @param call        Call data of the opening call. If call is NULL, the
- *                    connection was opened by accepting the
- *                    IPC_M_CONNECT_TO_ME call and this function is called
- *                    directly by the server.
+ * @param call        Call data of the opening call. If call is NULL, it's
+ *                    either a callback connection that was opened by
+ *                    accepting the IPC_M_CONNECT_TO_ME call.
+ *                    Alternatively, it is zero when we are opening
+ *                    implicit connection.
  * @param handler     Connection handler.
  * @param data        Client argument to pass to the connection handler.
@@ -1746,20 +1747,12 @@
 	}
 
-	async_sess_t *sess = calloc(1, sizeof(async_sess_t));
+	async_sess_t *sess = create_session(phandle, mgmt, 0, 0, 0);
 	if (sess == NULL) {
-		async_answer_0(&call, ENOMEM);
-		return NULL;
-	}
-
-	sess->iface = 0;
-	sess->mgmt = mgmt;
-	sess->phone = phandle;
-
-	fibril_mutex_initialize(&sess->remote_state_mtx);
-	list_initialize(&sess->exch_list);
-	fibril_mutex_initialize(&sess->mutex);
-
-	/* Acknowledge the connected phone */
-	async_answer_0(&call, EOK);
+		ipc_hangup(phone);
+		async_answer_0(&call, errno);
+	} else {
+		/* Acknowledge the connected phone */
+		async_answer_0(&call, EOK);
+	}
 
 	return sess;
@@ -1788,17 +1781,5 @@
 		return NULL;
 
-	async_sess_t *sess = calloc(1, sizeof(async_sess_t));
-	if (sess == NULL)
-		return NULL;
-
-	sess->iface = 0;
-	sess->mgmt = mgmt;
-	sess->phone = phandle;
-
-	fibril_mutex_initialize(&sess->remote_state_mtx);
-	list_initialize(&sess->exch_list);
-	fibril_mutex_initialize(&sess->mutex);
-
-	return sess;
+	return create_session(phandle, mgmt, 0, 0, 0);
 }
 
Index: uspace/lib/c/generic/libc.c
===================================================================
--- uspace/lib/c/generic/libc.c	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/libc.c	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -51,6 +51,6 @@
 #include "private/fibril.h"
 #include "private/malloc.h"
-#include "private/ns.h" // TODO maybe better filename for session_primary
 #include "private/task.h"
+#include "private/taskman.h"
 
 
@@ -63,4 +63,17 @@
 static bool env_setup;
 static fibril_t main_fibril;
+
+static void initialize_taskman(pcb_t *pcb)
+{
+	if (__pcb == NULL) {
+		async_sess_t *session_tm = taskman_connect();
+		if (session_tm == NULL) {
+			abort();
+		}
+		__task_init(session_tm);
+	} else {
+		__task_init(__pcb->session_taskman);
+	}
+}
 
 void __libc_main(void *pcb_ptr)
@@ -107,14 +120,9 @@
 #endif
 	
-	/* Setup async framework */
+	/* Setup async framework and taskman connection */
 	__async_server_init();
-	if (__pcb == NULL) {
-		__async_client_init(NULL);
-		__task_init(NULL);
-	} else {
-		__async_client_init(__pcb->session_primary);
-		__task_init(__pcb->session_taskman);
-	}
+	__async_client_init();
 	__async_ports_init();
+	initialize_taskman(__pcb);
 
 	/* The basic run-time environment is setup */
Index: uspace/lib/c/generic/loader.c
===================================================================
--- uspace/lib/c/generic/loader.c	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/loader.c	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -45,5 +45,7 @@
 #include <task.h>
 #include <vfs/vfs.h>
+
 #include "private/loader.h"
+#include "private/taskman.h"
 
 /** Connect to a new program loader.
@@ -66,7 +68,6 @@
 	if (ldr == NULL)
 		return NULL;
-
-	async_sess_t *sess =
-	    service_connect_blocking(SERVICE_TASKMAN, TASKMAN_CONNECT_TO_LOADER, 0);
+	
+	async_sess_t *sess = taskman_session_loader();
 	if (sess == NULL) {
 		free(ldr);
Index: uspace/lib/c/generic/ns.c
===================================================================
--- uspace/lib/c/generic/ns.c	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/ns.c	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -38,5 +38,24 @@
 #include <macros.h>
 #include <errno.h>
-#include "private/ns.h"
+
+#include "private/taskman.h"
+
+static async_sess_t *session_ns = NULL;
+
+static async_exch_t *ns_exchange_begin(void)
+{
+	/* Lazily connect to our NS */
+	if (session_ns == NULL) {
+		session_ns = taskman_session_ns();
+	}
+
+	async_exch_t *exch = async_exchange_begin(session_ns);
+	return exch;
+}
+
+static void ns_exchange_end(async_exch_t *exch)
+{
+	async_exchange_end(exch);
+}
 
 /*
@@ -49,8 +68,4 @@
     async_port_handler_t handler, void *data)
 {
-	async_sess_t *sess = get_session_primary();
-	if (sess == NULL)
-		return EIO;
-
 	port_id_t port;
 	errno_t rc = async_create_port(iface, handler, data, &port);
@@ -58,5 +73,5 @@
 		return rc;
 
-	async_exch_t *exch = async_exchange_begin(sess);
+	async_exch_t *exch = ns_exchange_begin();
 
 	ipc_call_t answer;
@@ -64,5 +79,5 @@
 	rc = async_connect_to_me(exch, iface, service, 0);
 
-	async_exchange_end(exch);
+	ns_exchange_end(exch);
 
 	if (rc != EOK) {
@@ -105,17 +120,13 @@
 async_sess_t *service_connect(service_t service, iface_t iface, sysarg_t arg3)
 {
-	async_sess_t *sess = get_session_primary();
-	if (sess == NULL)
-		return NULL;
-
-	async_exch_t *exch = async_exchange_begin(sess);
+	async_exch_t *exch = ns_exchange_begin();
 	if (exch == NULL)
 		return NULL;
 
-	async_sess_t *csess =
+	async_sess_t *sess =
 	    async_connect_me_to(exch, iface, service, arg3);
-	async_exchange_end(exch);
+	ns_exchange_end(exch);
 
-	if (csess == NULL)
+	if (sess == NULL)
 		return NULL;
 
@@ -125,5 +136,5 @@
 	 * first argument for non-initial connections.
 	 */
-	async_sess_args_set(csess, iface, arg3, 0);
+	async_sess_args_set(sess, iface, arg3, 0);
 
 	return csess;
@@ -133,14 +144,10 @@
     sysarg_t arg3)
 {
-	async_sess_t *sess = get_session_primary();
+	async_exch_t *exch = ns_exchange_begin();
+	async_sess_t *sess =
+	    async_connect_me_to_blocking(exch, iface, service, arg3);
+	ns_exchange_end(exch);
+
 	if (sess == NULL)
-		return NULL;
-
-	async_exch_t *exch = async_exchange_begin(sess);
-	async_sess_t *csess =
-	    async_connect_me_to_blocking(exch, iface, service, arg3);
-	async_exchange_end(exch);
-
-	if (csess == NULL)
 		return NULL;
 
@@ -150,38 +157,18 @@
 	 * first argument for non-initial connections.
 	 */
-	async_sess_args_set(csess, iface, arg3, 0);
+	async_sess_args_set(sess, iface, arg3, 0);
 
-	return csess;
+	return sess;
 }
 
 errno_t ns_ping(void)
 {
-	async_sess_t *sess = get_session_primary();
-	if (sess == NULL)
-		return EIO;
-
-	async_exch_t *exch = async_exchange_begin(sess);
+	async_exch_t *exch = ns_exchange_begin(sess);
 	errno_t rc = async_req_0_0(exch, NS_PING);
-	async_exchange_end(exch);
+	ns_exchange_end(exch);
 
 	return rc;
 }
 
-
-async_sess_t *get_session_primary(void)
-{
-	async_exch_t *exch;
-
-	if (sess_primary == NULL) {
-		exch = async_exchange_begin(&session_primary);
-		sess_primary = async_connect_me_to(exch, 0, 0, 0);
-		async_exchange_end(exch);
-		if (sess_primary == NULL)
-			return NULL;
-	}
-
-	return sess_primary;
-}
-
 /** @}
  */
Index: uspace/lib/c/generic/private/async.h
===================================================================
--- uspace/lib/c/generic/private/async.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/private/async.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -96,5 +96,5 @@
 extern void __async_server_init(void);
 extern void __async_server_fini(void);
-extern void __async_client_init(async_sess_t *);
+extern void __async_client_init(void);
 extern void __async_client_fini(void);
 extern void __async_ports_init(void);
@@ -106,4 +106,6 @@
 
 extern void async_reply_received(ipc_call_t *);
+extern async_sess_t *create_session(int, exch_mgmt_t,
+	sysarg_t, sysarg_t, sysarg_t);
 
 #endif
Index: uspace/lib/c/generic/private/ns.h
===================================================================
--- uspace/lib/c/generic/private/ns.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ 	(revision )
@@ -1,45 +1,0 @@
-/*
- * Copyright (c) 2011 Martin Decky
- * 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 libc
- * @{
- */
-/** @file
- */
-
-#ifndef _LIBC_PRIVATE_NS_H_
-#define _LIBC_PRIVATE_NS_H_
-
-#include <async.h>
-
-extern async_sess_t *session_primary;
-
-#endif
-
-/** @}
- */
Index: uspace/lib/c/generic/private/task.h
===================================================================
--- uspace/lib/c/generic/private/task.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/private/task.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -36,13 +36,5 @@
 #define LIBC_PRIVATE_TASK_H_
 
-#include <async.h>
-
-void __task_init(async_sess_t *);
-
 int task_retval_internal(int, bool);
-
-async_exch_t *taskman_exchange_begin(void);
-
-void taskman_exchange_end(async_exch_t *);
 
 #endif
Index: uspace/lib/c/generic/private/taskman.h
===================================================================
--- uspace/lib/c/generic/private/taskman.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
+++ uspace/lib/c/generic/private/taskman.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2015 Michal Koutny
+ * 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 libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_PRIVATE_TASKMAN_H_
+#define LIBC_PRIVATE_TASKMAN_H_
+
+#include <async.h>
+
+extern async_sess_t *session_taskman;
+
+void __task_init(async_sess_t *);
+
+async_exch_t *taskman_exchange_begin(void);
+void taskman_exchange_end(async_exch_t *);
+
+extern async_sess_t *taskman_connect(void);
+extern async_sess_t *taskman_session_ns(void);
+extern async_sess_t *taskman_session_loader(void);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/generic/task.c
===================================================================
--- uspace/lib/c/generic/task.c	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/task.c	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -49,9 +49,9 @@
 #include <str.h>
 #include <task.h>
+#include <taskman.h>
 #include <vfs/vfs.h>
-#include "private/ns.h"
+
 #include "private/task.h"
-
-static async_sess_t *session_taskman = NULL;
+#include "private/taskman.h"
 
 task_id_t task_get_id(void)
@@ -69,28 +69,4 @@
 }
 
-async_exch_t *taskman_exchange_begin(void)
-{
-	/* Lazy connection */
-	if (session_taskman == NULL) {
-		// TODO unify exchange mgmt with taskman_handshake/__init
-		session_taskman = service_connect_blocking(EXCHANGE_SERIALIZE,
-		    SERVICE_TASKMAN,
-		    TASKMAN_CONTROL,
-		    0);
-	}
-
-	if (session_taskman == NULL) {
-		return NULL;
-	}
-
-	async_exch_t *exch = async_exchange_begin(session_taskman);
-	return exch;
-}
-
-void taskman_exchange_end(async_exch_t *exch)
-{
-	async_exchange_end(exch);
-}
-
 /** Set the task name.
  *
@@ -129,5 +105,4 @@
  *
  * @return EOK on success, else error code.
- * @return TODO check this doesn't return EINVAL -- clash with task_wait
  */
 static errno_t task_setup_wait(task_id_t id, task_wait_t *wait)
@@ -393,10 +368,13 @@
  * (it can be reused, but must be reinitialized with task_setup_wait first)
  *
- * @param wait   task_wait_t previously initialized by task_setup_wait.
- * @param texit  Store type of task exit here.
- * @param retval Store return value of the task here.
+ * @param[in/out] wait   task_wait_t previously initialized by task_setup_wait
+ *                       or returned by task_wait with non-zero flags. the
+ *                       flags are updated so that they represent what can be
+ *                       still waited for.
+ * @param[out]    texit  Store type of task exit here.
+ * @param[out]    retval Store return value of the task here.
  *
  * @return EOK on success
- * @return EINVAL on lost wait TODO other error codes
+ * @return EINTR on lost wait
  */
 errno_t task_wait(task_wait_t *wait, task_exit_t *texit, int *retval)
@@ -405,5 +383,5 @@
 	async_wait_for(wait->aid, &rc);
 
-	if (rc == EOK || rc == EINVAL) {
+	if (rc == EOK || rc == EINTR) {
 		if (wait->flags & TASK_WAIT_EXIT && texit)
 			*texit = ipc_get_arg1(wait->result);
@@ -471,10 +449,4 @@
 }
 
-void __task_init(async_sess_t *sess)
-{
-	assert(session_taskman == NULL);
-	session_taskman = sess;
-}
-
 /** @}
  */
Index: uspace/lib/c/generic/task_event.c
===================================================================
--- uspace/lib/c/generic/task_event.c	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/task_event.c	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -38,5 +38,5 @@
 #include <task.h>
 
-#include "private/task.h"
+#include "private/taskman.h"
 
 static task_event_handler_t task_event_handler = NULL;
@@ -65,5 +65,5 @@
 
 		if (!IPC_GET_IMETHOD(call)) {
-			/* Hangup, TODO explain or handle differntly */
+			/* Hangup, end of game */
 			break;
 		}
Index: uspace/lib/c/generic/taskman.c
===================================================================
--- uspace/lib/c/generic/taskman.c	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/generic/taskman.c	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -33,16 +33,99 @@
  */
 
+
+#include <async.h>
 #include <errno.h>
+#include <ipc/common.h>
 #include <ipc/taskman.h>
+#include <task.h>
 #include <taskman.h>
 
-#include <stdio.h>
+#include "private/async.h"
+#include "private/taskman.h"
 
-//TODO better filename?
-#include "private/ns.h"
+async_sess_t *session_taskman = NULL;
 
-static int taskman_ask_callback(async_sess_t *session_tm)
+void __task_init(async_sess_t *sess)
 {
-	async_exch_t *exch = async_exchange_begin(session_tm);
+	assert(session_taskman == NULL);
+	session_taskman = sess;
+}
+
+async_exch_t *taskman_exchange_begin(void)
+{
+	assert(session_taskman);
+
+	async_exch_t *exch = async_exchange_begin(session_taskman);
+	return exch;
+}
+
+void taskman_exchange_end(async_exch_t *exch)
+{
+	async_exchange_end(exch);
+}
+
+/** Wrap PHONE_INITIAL with session and introduce to taskman
+ */
+async_sess_t *taskman_connect(void)
+{
+	/*
+	 *  EXCHANGE_ATOMIC would require single calls only,
+	 *  EXCHANGE_PARALLEL not sure about implementation via multiple phones,
+	 * >EXCHANGE_SERIALIZE perhaphs no harm, except the client serialization
+	 */
+	const exch_mgmt_t mgmt = EXCHANGE_SERIALIZE;
+	async_sess_t *sess = create_session(PHONE_INITIAL, mgmt, 0, 0, 0);
+
+	if (sess != NULL) {
+		/* Introduce ourselves and ignore answer */
+		async_exch_t *exch = async_exchange_begin(sess);
+		aid_t req = async_send_0(exch, TASKMAN_NEW_TASK, NULL);
+		async_exchange_end(exch);
+		
+		if (req) {
+			async_forget(req);
+		}
+	}
+
+	return sess;
+}
+
+/** Ask taskman to pass/share its NS */
+async_sess_t *taskman_session_ns(void)
+{
+	assert(session_taskman);
+
+	async_exch_t *exch = async_exchange_begin(session_taskman);
+	assert(exch);
+
+	async_sess_t *sess = async_connect_me_to(EXCHANGE_ATOMIC,
+	    exch, TASKMAN_CONNECT_TO_NS, 0, 0);
+	async_exchange_end(exch);
+
+	return sess;
+}
+
+/** Ask taskman to connect to (a new) loader instance */
+async_sess_t *taskman_session_loader(void)
+{
+	assert(session_taskman);
+
+	async_exch_t *exch = async_exchange_begin(session_taskman);
+	async_sess_t *sess = async_connect_me_to(EXCHANGE_SERIALIZE,
+	    exch, TASKMAN_CONNECT_TO_LOADER, 0, 0);
+	async_exchange_end(exch);
+
+	return sess;
+}
+
+/** Introduce as loader to taskman
+ *
+ * @return EOK on success, otherwise propagated error code
+ */
+int taskman_intro_loader(void)
+{
+	assert(session_taskman);
+
+	async_exch_t *exch = async_exchange_begin(session_taskman);
 	int rc = async_connect_to_me(
 	    exch, TASKMAN_LOADER_CALLBACK, 0, 0, NULL, NULL);
@@ -52,38 +135,33 @@
 }
 
-static async_sess_t *taskman_connect_to_ns(async_sess_t *session_tm)
+/** Tell taskman we are his NS
+ *
+ * @return EOK on success, otherwise propagated error code
+ */
+int taskman_intro_ns(void)
 {
-	async_exch_t *exch = async_exchange_begin(session_tm);
-	async_sess_t *session_ns = async_connect_me_to(EXCHANGE_ATOMIC,
-	    exch, TASKMAN_LOADER_TO_NS, 0, 0);
-	async_exchange_end(exch);
+	assert(session_taskman);
 
-	return session_ns;
+	async_exch_t *exch = async_exchange_begin(session_taskman);
+	aid_t req = async_send_0(exch, TASKMAN_I_AM_NS, NULL);
+
+	int rc = async_connect_to_me(exch, 0, 0, 0, NULL, NULL);
+	taskman_exchange_end(exch);
+
+	if (rc != EOK) {
+		return rc;
+	}
+
+	sysarg_t retval;
+	async_wait_for(req, &retval);
+	return retval;
 }
 
-/** Set up phones upon being spawned by taskman
- *
- * Assumes primary session exists that is connected to taskman.
- * After handshake, taskman is connected to us (see, it's opposite) and broker
- * session is set up according to taskman.
- *
- *
- * @return Session to broker (naming service) or NULL (sets errno).
- */
-async_sess_t *taskman_handshake(void)
+async_sess_t *taskman_get_session(void)
 {
-	int rc = taskman_ask_callback(session_primary);
-	if (rc != EOK) {
-		errno = rc;
-		return NULL;
-	}
+	return session_taskman;
+}
 
-	async_sess_t *session_ns = taskman_connect_to_ns(session_primary);
-	if (session_ns == NULL) {
-		errno = ENOENT;
-	}
 
-	return session_ns;
-}
 
 /** @}
Index: uspace/lib/c/include/async.h
===================================================================
--- uspace/lib/c/include/async.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/include/async.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -276,5 +276,4 @@
 extern sysarg_t async_get_label(void);
 
-extern async_sess_t *async_session_primary_swap(async_sess_t *);
 extern async_sess_t *async_connect_me_to(async_exch_t *, iface_t, sysarg_t,
     sysarg_t);
Index: uspace/lib/c/include/ipc/common.h
===================================================================
--- uspace/lib/c/include/ipc/common.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/include/ipc/common.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -39,4 +39,7 @@
 #include <abi/ipc/ipc.h>
 
+/* Well known phone descriptors */
+#define PHONE_INITIAL  0
+
 #define IPC_FLAG_BLOCKING   0x01
 // TODO autostart flag may be united with blocking, this should be later made
Index: uspace/lib/c/include/ipc/services.h
===================================================================
--- uspace/lib/c/include/ipc/services.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/include/ipc/services.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -46,5 +46,4 @@
 	SERVICE_LOC        = FOURCC('l', 'o', 'c', ' '),
 	SERVICE_SYSMAN     = FOURCC('s', 'y', 's', 'm'),
-	SERVICE_TASKMAN    = FOURCC('t', 's', 'k', 'm'),
 	SERVICE_LOGGER     = FOURCC('l', 'o', 'g', 'g'),
 	SERVICE_DEVMAN     = FOURCC('d', 'e', 'v', 'n'),
Index: uspace/lib/c/include/ipc/taskman.h
===================================================================
--- uspace/lib/c/include/ipc/taskman.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/include/ipc/taskman.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -42,5 +42,7 @@
 	TASKMAN_WAIT = IPC_FIRST_USER_METHOD,
 	TASKMAN_RETVAL,
-	TASKMAN_EVENT_CALLBACK
+	TASKMAN_EVENT_CALLBACK,
+	TASKMAN_NEW_TASK,
+	TASKMAN_I_AM_NS
 } taskman_request_t;
 
@@ -50,9 +52,8 @@
 
 typedef enum {
-	TASKMAN_CONNECT_TO_LOADER = 0,
-	TASKMAN_LOADER_TO_NS,
-	TASKMAN_LOADER_CALLBACK,
-	TASKMAN_CONTROL
-} taskman_interface_t;
+	TASKMAN_CONNECT_TO_NS = 0,
+	TASKMAN_CONNECT_TO_LOADER,
+	TASKMAN_LOADER_CALLBACK
+} taskman_connect_t;
 
 #endif
Index: uspace/lib/c/include/loader/pcb.h
===================================================================
--- uspace/lib/c/include/loader/pcb.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/include/loader/pcb.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -61,7 +61,4 @@
 	entry_point_t entry;
 
-	/** Primary session to broker. */
-	async_sess_t *session_primary;
-	
 	/** Session to taskman (typically spawn parent) */
 	async_sess_t *session_taskman;
Index: uspace/lib/c/include/ns.h
===================================================================
--- uspace/lib/c/include/ns.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/include/ns.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -49,4 +49,6 @@
 extern async_sess_t *ns_session_get(void);
 
+extern async_sess_t *ns_get_session(void);
+
 #endif
 
Index: uspace/lib/c/include/task.h
===================================================================
--- uspace/lib/c/include/task.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/include/task.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -76,5 +76,5 @@
 extern errno_t task_retval(int);
 
-/* Implemented in task_event.h */
+/* Implemented in task_event.c */
 extern int task_register_event_handler(task_event_handler_t);
 
Index: uspace/lib/c/include/taskman.h
===================================================================
--- uspace/lib/c/include/taskman.h	(revision 456f7aec147ab77d57de3a2997e634873236b280)
+++ uspace/lib/c/include/taskman.h	(revision 2df7d82445fd27fa3d7738624e0591a3d59ae237)
@@ -36,7 +36,16 @@
 #define LIBC_TASKMAN_H_
 
+#ifndef TASKMAN_DISABLE_ASYNC
 #include <async.h>
+#endif
 
-extern async_sess_t *taskman_handshake(void);
+/* Internal functions to be used by loader only */
+#ifndef TASKMAN_DISABLE_ASYNC
+extern async_sess_t *taskman_get_session(void);
+#endif
+extern int taskman_intro_loader(void);
+
+/* Internal functions to be used by NS only */
+extern int taskman_intro_ns(void);
 
 #endif
