diff --git a/Makefile.in b/Makefile.in
index b8de23eb06071d002d91effee0463025c1f5f24c..efb64d62b8c1ea86b0aab7addaa4642bf7fbb9a5 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -27,7 +27,7 @@ SVROBJS=svr-kex.o svr-algo.o svr-auth.o sshpty.o \
 		svr-chansession.o svr-runopts.o svr-agentfwd.o svr-main.o svr-x11fwd.o
 
 CLIOBJS=cli-algo.o cli-main.o cli-auth.o cli-authpasswd.o cli-kex.o \
-		cli-session.o cli-service.o cli-runopts.o
+		cli-session.o cli-service.o cli-runopts.o cli-chansession.o
 
 CLISVROBJS=common-session.o packet.o common-algo.o common-kex.o \
 			common-channel.o common-chansession.o termcodes.o loginrec.o \
diff --git a/cli-auth.c b/cli-auth.c
index 3759ff588476617e5148f490c86ba45c4da801b4..37e78144e58c651ecdbbe053e31424e6d3a311bc 100644
--- a/cli-auth.c
+++ b/cli-auth.c
@@ -96,6 +96,7 @@ void recv_msg_userauth_failure() {
 void recv_msg_userauth_success() {
 	TRACE(("received msg_userauth_success"));
 	ses.authstate.authdone = 1;
+	cli_ses.state = USERAUTH_SUCCESS_RCVD;
 }
 
 void cli_auth_try() {
diff --git a/cli-session.c b/cli-session.c
index c999aed87d6580cd560873c9e7133abe3cabcd00..b126b278c51c913b4ed322190d26d2efc2892705 100644
--- a/cli-session.c
+++ b/cli-session.c
@@ -37,7 +37,6 @@ static const packettype cli_packettypes[] = {
 };
 
 static const struct ChanType *cli_chantypes[] = {
-//	&clichansess,
 	/* &chan_tcpdirect etc, though need to only allow if we've requested
 	 * that forwarding */
 	NULL /* Null termination */
@@ -148,6 +147,20 @@ static void cli_sessionloop() {
 			TRACE(("leave cli_sessionloop: cli_auth_try"));
 			return;
 
+			/*
+		case USERAUTH_SUCCESS_RCVD:
+			send_msg_service_request(SSH_SERVICE_CONNECTION);
+			cli_ses.state = SERVICE_CONN_REQ_SENT;
+			TRACE(("leave cli_sessionloop: sent ssh-connection service req"));
+			return;
+			*/
+
+		case USERAUTH_SUCCESS_RCVD:
+			cli_send_chansess_request();
+			TRACE(("leave cli_sessionloop: cli_send_chansess_request"));
+			cli_ses.state = SESSION_RUNNING;
+			return;
+
 		/* XXX more here needed */
 
 
diff --git a/common-channel.c b/common-channel.c
index 3eea044c1324c6ccb3321d64ff22aabe542b9831..fbfb00b88cd2471a15033a433caa76a0a17cf062 100644
--- a/common-channel.c
+++ b/common-channel.c
@@ -67,6 +67,7 @@ void chaninitialise(const struct ChanType *chantypes[]) {
 	ses.channels = (struct Channel**)m_malloc(sizeof(struct Channel*));
 	ses.chansize = 1;
 	ses.channels[0] = NULL;
+	ses.chancount = 0;
 
 	ses.chantypes = chantypes;
 
@@ -153,6 +154,7 @@ struct Channel* newchannel(unsigned int remotechan,
 	newchan->recvmaxpacket = RECV_MAXPACKET;
 
 	ses.channels[i] = newchan;
+	ses.chancount++;
 
 	TRACE(("leave newchannel"));
 
@@ -515,6 +517,7 @@ static void deletechannel(struct Channel *channel) {
 
 	ses.channels[channel->index] = NULL;
 	m_free(channel);
+	ses.chancount--;
 	
 }
 
@@ -934,6 +937,7 @@ void recv_msg_channel_open_confirmation() {
 
 	unsigned int chan;
 	struct Channel * channel;
+	int ret;
 
 	TRACE(("enter recv_msg_channel_open_confirmation"));
 	chan = buf_getint(ses.payload);
@@ -949,6 +953,15 @@ void recv_msg_channel_open_confirmation() {
 	
 	TRACE(("new chan remote %d localho %d", channel->remotechan, chan));
 
+	/* Run the inithandler callback */
+	if (channel->type->inithandler) {
+		ret = channel->type->inithandler(channel);
+		if (ret > 0) {
+			removechannel(channel);
+			TRACE(("inithandler returned failure %d", ret));
+		}
+	}
+
 	
 	TRACE(("leave recv_msg_channel_open_confirmation"));
 }
diff --git a/runopts.h b/runopts.h
index 125d737ebd5d6f425ca33e70ead5fe019d625948..b2fc1495ff7ecd30ab916a7d94e35f670efba750 100644
--- a/runopts.h
+++ b/runopts.h
@@ -83,6 +83,9 @@ typedef struct cli_runopts {
 	char *remoteport;
 
 	char *username;
+
+	char *cmd;
+	int wantpty;
 	/* XXX TODO */
 
 } cli_runopts;
diff --git a/session.h b/session.h
index bf6adb3e81f0ee93446250e4fda85c0d59fa5b5a..349c00f77d41c0da68d346561235be43dcb58b1c 100644
--- a/session.h
+++ b/session.h
@@ -152,6 +152,7 @@ struct sshsession {
 	/* Channel related */
 	struct Channel ** channels; /* these pointers may be null */
 	unsigned int chansize; /* the number of Channel*s allocated for channels */
+	unsigned int chancount; /* the number of Channel*s in use */
 	const struct ChanType **chantypes; /* The valid channel types */
 
 	
@@ -194,6 +195,8 @@ typedef enum {
 	USERAUTH_METHODS_SENT,
 	USERAUTH_REQ_SENT,
 	USERAUTH_FAIL_RCVD,
+	USERAUTH_SUCCESS_RCVD,
+	SESSION_RUNNING,
 
 } cli_state;