Index: protocols/msn/msn.c
===================================================================
--- protocols/msn/msn.c	(revision 02bb9db2edd535036e030e004a58ed1459c15bb8)
+++ protocols/msn/msn.c	(revision 64768d4ec0c3ad473573c3f3c34871e0081b4e59)
@@ -154,6 +154,5 @@
 	if( strcmp( who, "raw" ) == 0 )
 	{
-		msn_write( ic, message, strlen( message ) );
-		msn_write( ic, "\r\n", 2 );
+		msn_ns_write( ic, -1, "%s\r\n", message );
 	}
 	else
@@ -193,5 +192,4 @@
 static void msn_set_away( struct im_connection *ic, char *state, char *message )
 {
-	char buf[1024];
 	char *uux;
 	struct msn_data *md = ic->proto_data;
@@ -202,13 +200,11 @@
 		md->away_state = msn_away_state_list + 1;
 	
-	g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, md->away_state->code );
-	if( !msn_write( ic, buf, strlen( buf ) ) )
+	if( !msn_ns_write( ic, -1, "CHG %d %s\r\n", ++md->trId, md->away_state->code ) )
 		return;
 	
 	uux = g_markup_printf_escaped( "<Data><PSM>%s</PSM><CurrentMedia></CurrentMedia>"
 	                               "</Data>", message ? message : "" );
-	g_snprintf( buf, sizeof( buf ), "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux );
-	if( !msn_write( ic, buf, strlen( buf ) ) )
-		return;
+	msn_ns_write( ic, -1, "UUX %d %zd\r\n%s", ++md->trId, strlen( uux ), uux );
+	g_free( uux );
 }
 
@@ -290,5 +286,5 @@
 static void msn_keepalive( struct im_connection *ic )
 {
-	msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) );
+	msn_ns_write( ic, -1, "PNG\r\n" );
 }
 
Index: protocols/msn/msn.h
===================================================================
--- protocols/msn/msn.h	(revision 02bb9db2edd535036e030e004a58ed1459c15bb8)
+++ protocols/msn/msn.h	(revision 64768d4ec0c3ad473573c3f3c34871e0081b4e59)
@@ -208,4 +208,5 @@
 
 /* ns.c */
+int msn_ns_write( struct im_connection *ic, int fd, const char *fmt, ... );
 gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond );
 void msn_auth_got_passport_token( struct im_connection *ic, const char *token, const char *error );
@@ -214,5 +215,4 @@
 
 /* msn_util.c */
-int msn_write( struct im_connection *ic, char *s, int len );
 int msn_logged_in( struct im_connection *ic );
 int msn_buddy_list_add( struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *realname_, const char *group );
@@ -236,5 +236,5 @@
 
 /* sb.c */
-int msn_sb_write( struct msn_switchboard *sb, char *s, int len );
+int msn_sb_write( struct msn_switchboard *sb, const char *fmt, ... );
 struct msn_switchboard *msn_sb_create( struct im_connection *ic, char *host, int port, char *key, int session );
 struct msn_switchboard *msn_sb_by_handle( struct im_connection *ic, char *handle );
Index: protocols/msn/msn_util.c
===================================================================
--- protocols/msn/msn_util.c	(revision 02bb9db2edd535036e030e004a58ed1459c15bb8)
+++ protocols/msn/msn_util.c	(revision 64768d4ec0c3ad473573c3f3c34871e0081b4e59)
@@ -30,26 +30,4 @@
 #include <ctype.h>
 
-int msn_write( struct im_connection *ic, char *s, int len )
-{
-	struct msn_data *md = ic->proto_data;
-	int st;
-	
-	if( getenv( "BITLBEE_DEBUG" ) )
-	{
-		write( 2, "->NS:", 5 );
-		write( 2, s, len );
-	}
-	
-	st = write( md->fd, s, len );
-	if( st != len )
-	{
-		imcb_error( ic, "Short write() to main server" );
-		imc_logout( ic, TRUE );
-		return 0;
-	}
-	
-	return 1;
-}
-
 int msn_logged_in( struct im_connection *ic )
 {
@@ -76,5 +54,5 @@
 {
 	struct msn_data *md = ic->proto_data;
-	char buf[1024], groupid[8];
+	char groupid[8];
 	bee_user_t *bu;
 	struct msn_buddy_data *bd;
@@ -144,9 +122,9 @@
 	if( ( adl = adlrml_entry( who, list ) ) )
 	{
-		g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n%s",
-		            ++md->trId, strlen( adl ), adl );
+		int st = msn_ns_write( ic, -1, "ADL %d %zd\r\n%s",
+		                       ++md->trId, strlen( adl ), adl );
 		g_free( adl );
 		
-		return msn_write( ic, buf, strlen( buf ) );
+		return st;
 	}
 	
@@ -157,5 +135,5 @@
 {
 	struct msn_data *md = ic->proto_data;
-	char buf[1024], groupid[8];
+	char groupid[8];
 	bee_user_t *bu;
 	struct msn_buddy_data *bd;
@@ -189,9 +167,9 @@
 	if( ( adl = adlrml_entry( who, list ) ) )
 	{
-		g_snprintf( buf, sizeof( buf ), "RML %d %zd\r\n%s",
-		            ++md->trId, strlen( adl ), adl );
+		int st = msn_ns_write( ic, -1, "RML %d %zd\r\n%s",
+		                       ++md->trId, strlen( adl ), adl );
 		g_free( adl );
 		
-		return msn_write( ic, buf, strlen( buf ) );
+		return st;
 	}
 	
@@ -603,13 +581,10 @@
 	struct msn_data *md = ic->proto_data;
 	char fn[strlen(value)*3+1];
-	char buf[512];
 	
 	strcpy( fn, value );
 	http_encode( fn );
-	g_snprintf( buf, sizeof( buf ), "PRP %d MFN %s\r\n",
-	            ++md->trId, fn );
 	
 	/* Note: We don't actually know if the server accepted the new name,
 	   and won't give proper feedback yet if it doesn't. */
-	return msn_write( ic, buf, strlen( buf ) );
-}
+	return msn_ns_write( ic, -1, "PRP %d MFN %s\r\n", ++md->trId, fn );
+}
Index: protocols/msn/ns.c
===================================================================
--- protocols/msn/ns.c	(revision 02bb9db2edd535036e030e004a58ed1459c15bb8)
+++ protocols/msn/ns.c	(revision 64768d4ec0c3ad473573c3f3c34871e0081b4e59)
@@ -38,9 +38,39 @@
 static void msn_ns_send_adl( struct im_connection *ic );
 
+int msn_ns_write( struct im_connection *ic, int fd, const char *fmt, ... )
+{
+	struct msn_data *md = ic->proto_data;
+	va_list params;
+	char *out;
+	size_t len;
+	int st;
+	
+	va_start( params, fmt );
+	out = g_strdup_vprintf( fmt, params );
+	va_end( params );
+	
+	if( fd < 0 )
+		fd = md->fd;
+	
+	if( getenv( "BITLBEE_DEBUG" ) )
+		fprintf( stderr, "->NS%d:%s", fd, out );
+	
+	len = strlen( out );
+	st = write( fd, out, len );
+	g_free( out );
+	if( st != len )
+	{
+		imcb_error( ic, "Short write() to main server" );
+		imc_logout( ic, TRUE );
+		return 0;
+	}
+	
+	return 1;
+}
+
 gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond )
 {
 	struct im_connection *ic = data;
 	struct msn_data *md;
-	char s[1024];
 	
 	if( !g_slist_find( msn_connections, ic ) )
@@ -74,6 +104,5 @@
 	md->handler->rxq = g_new0( char, 1 );
 	
-	g_snprintf( s, sizeof( s ), "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER );
-	if( msn_write( ic, s, strlen( s ) ) )
+	if( msn_ns_write( ic, -1, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER ) )
 	{
 		ic->inpa = b_input_add( md->fd, B_EV_IO_READ, msn_ns_callback, ic );
@@ -104,5 +133,4 @@
 	struct im_connection *ic = data;
 	struct msn_data *md = ic->proto_data;
-	char buf[1024];
 	
 	if( num_parts == 0 )
@@ -121,13 +149,11 @@
 		}
 		
-		g_snprintf( buf, sizeof( buf ), "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n",
-		                                ++md->trId, ic->acc->user );
-		return( msn_write( ic, buf, strlen( buf ) ) );
+		return( msn_ns_write( ic, -1, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n",
+		                      ++md->trId, ic->acc->user ) );
 	}
 	else if( strcmp( cmd[0], "CVR" ) == 0 )
 	{
 		/* We don't give a damn about the information we just received */
-		g_snprintf( buf, sizeof( buf ), "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user );
-		return( msn_write( ic, buf, strlen( buf ) ) );
+		return msn_ns_write( ic, -1, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user );
 	}
 	else if( strcmp( cmd[0], "XFR" ) == 0 )
@@ -280,4 +306,5 @@
 	{
 		char *resp;
+		int st;
 		
 		if( num_parts < 3 )
@@ -289,10 +316,10 @@
 		
 		resp = msn_p11_challenge( cmd[2] );
-		g_snprintf( buf, sizeof( buf ), "QRY %d %s %zd\r\n%s",
-		            ++md->trId, MSNP11_PROD_ID,
-		            strlen( resp ), resp );
+		
+		st =  msn_ns_write( ic, -1, "QRY %d %s %zd\r\n%s",
+		                    ++md->trId, MSNP11_PROD_ID,
+		                    strlen( resp ), resp );
 		g_free( resp );
-		
-		return( msn_write( ic, buf, strlen( buf ) ) );
+		return st;
 	}
 	else if( strcmp( cmd[0], "ILN" ) == 0 )
@@ -699,8 +726,5 @@
 	if( token )
 	{
-		char buf[1536];
-		
-		g_snprintf( buf, sizeof( buf ), "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token );
-		msn_write( ic, buf, strlen( buf ) );
+		msn_ns_write( ic, -1, "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token );
 	}
 	else
@@ -713,5 +737,4 @@
 void msn_auth_got_contact_list( struct im_connection *ic )
 {
-	char buf[64];
 	struct msn_data *md;
 	
@@ -721,8 +744,5 @@
 	
 	md = ic->proto_data;
-	
-	
-	g_snprintf( buf, sizeof( buf ), "BLP %d %s\r\n", ++md->trId, "BL" );
-	msn_write( ic, buf, strlen( buf ) );
+	msn_ns_write( ic, -1, "BLP %d %s\r\n", ++md->trId, "BL" );
 }
 
@@ -770,5 +790,5 @@
 	struct xt_node *adl;
 	struct msn_data *md = ic->proto_data;
-	char *adls, buf[64];
+	char *adls;
 	
 	adl = xt_new_node( "ml", NULL, NULL );
@@ -782,10 +802,7 @@
 		return;
 	}
+	
 	adls = xt_to_string( adl );
-	
-	g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n", ++md->trId, strlen( adls ) );
-	if( msn_write( ic, buf, strlen( buf ) ) )
-		msn_write( ic, adls, strlen( adls ) );
-	
+	msn_ns_write( ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen( adls ), adls );
 	g_free( adls );
 }
Index: protocols/msn/sb.c
===================================================================
--- protocols/msn/sb.c	(revision 02bb9db2edd535036e030e004a58ed1459c15bb8)
+++ protocols/msn/sb.c	(revision 64768d4ec0c3ad473573c3f3c34871e0081b4e59)
@@ -35,22 +35,28 @@
 static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts );
 
-int msn_sb_write( struct msn_switchboard *sb, char *s, int len )
-{
+int msn_sb_write( struct msn_switchboard *sb, const char *fmt, ... )
+{
+	va_list params;
+	char *out;
+	size_t len;
 	int st;
 	
+	va_start( params, fmt );
+	out = g_strdup_vprintf( fmt, params );
+	va_end( params );
+	
 	if( getenv( "BITLBEE_DEBUG" ) )
-	{
-		write( 2, "->SB:", 5 );
-		write( 2, s, len );
-	}
-	
-	st = write( sb->fd, s, len );
+		fprintf( stderr, "->SB%d:%s", sb->fd, out );
+	
+	len = strlen( out );
+	st = write( sb->fd, out, len );
+	g_free( out );
 	if( st != len )
 	{
 		msn_sb_destroy( sb );
-		return( 0 );
-	}
-	
-	return( 1 );
+		return 0;
+	}
+	
+	return 1;
 }
 
@@ -59,5 +65,4 @@
 	struct msn_data *md = ic->proto_data;
 	struct msn_switchboard *sb;
-	char buf[1024];
 
 	/* FIXME: *CHECK* the reliability of using spare sb's! */
@@ -67,6 +72,5 @@
 		
 		sb->who = g_strdup( m->who );
-		g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, m->who );
-		if( msn_sb_write( sb, buf, strlen( buf ) ) )
+		if( msn_sb_write( sb, "CAL %d %s\r\n", ++sb->trId, m->who ) )
 		{
 			/* He/She should join the switchboard soon, let's queue the message. */
@@ -79,6 +83,5 @@
 	
 	/* If we reach this line, there was no spare switchboard, so let's make one. */
-	g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId );
-	if( !msn_write( ic, buf, strlen( buf ) ) )
+	if( !msn_ns_write( ic, -1, "XFR %d SB\r\n", ++md->trId ) )
 	{
 		g_free( m->who );
@@ -171,5 +174,5 @@
 	if( sb->ready )
 	{
-		char *packet, *buf;
+		char *buf;
 		int i, j;
 		
@@ -207,15 +210,13 @@
 		
 		/* Build the final packet (MSG command + the message). */
-		packet = g_strdup_printf( "MSG %d N %d\r\n%s", ++sb->trId, i, buf );
-		g_free( buf );
-		if( msn_sb_write( sb, packet, strlen( packet ) ) )
-		{
-			g_free( packet );
-			return( 1 );
+		if( msn_sb_write( sb, "MSG %d N %d\r\n%s", ++sb->trId, i, buf ) )
+		{
+			g_free( buf );
+			return 1;
 		}
 		else
 		{
-			g_free( packet );
-			return( 0 );
+			g_free( buf );
+			return 0;
 		}
 	}
@@ -332,5 +333,5 @@
 		g_snprintf( buf, sizeof( buf ), "ANS %d %s %s %d\r\n", ++sb->trId, ic->acc->user, sb->key, sb->session );
 	
-	if( msn_sb_write( sb, buf, strlen( buf ) ) )
+	if( msn_sb_write( sb, "%s", buf ) )
 		sb->inp = b_input_add( sb->fd, B_EV_IO_READ, msn_sb_callback, sb );
 	else
@@ -352,5 +353,4 @@
 	{
 		time_t now = time( NULL );
-		char buf[1024];
 		
 		if( now - md->first_sb_failure > 600 )
@@ -384,6 +384,5 @@
 		debug( "Moved queued messages back to the main queue, "
 		       "creating a new switchboard to retry." );
-		g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId );
-		if( !msn_write( ic, buf, strlen( buf ) ) )
+		if( !msn_ns_write( ic, -1, "XFR %d SB\r\n", ++md->trId ) )
 			return FALSE;
 	}
@@ -397,5 +396,4 @@
 	struct msn_switchboard *sb = data;
 	struct im_connection *ic = sb->ic;
-	char buf[1024];
 	
 	if( !num_parts )
@@ -426,12 +424,7 @@
 		
 		if( sb->who )
-		{
-			g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, sb->who );
-			return( msn_sb_write( sb, buf, strlen( buf ) ) );
-		}
+			return msn_sb_write( sb, "CAL %d %s\r\n", ++sb->trId, sb->who );
 		else
-		{
 			debug( "Just created a switchboard, but I don't know what to do with it." );
-		}
 	}
 	else if( strcmp( cmd[0], "IRO" ) == 0 )
