Index: lib/events.h
===================================================================
--- lib/events.h	(revision afb9ea93adb5805b580c343bd81cc168697d1d63)
+++ lib/events.h	(revision d150a9df0b0e84a3fe30af340762f96475f4b1e5)
@@ -81,9 +81,7 @@
 G_MODULE_EXPORT void b_event_remove(gint id);
 
-/* For now, closesocket() is only a function when using libevent. With GLib
-   it's a preprocessor macro. */
-#ifdef EVENTS_LIBEVENT
+/* With libevent, this one also cleans up event handlers if that wasn't already
+   done (the caller is expected to do so but may miss it sometimes). */
 G_MODULE_EXPORT void closesocket(int fd);
-#endif
 
 #endif /* _EVENTS_H_ */
Index: lib/events_glib.c
===================================================================
--- lib/events_glib.c	(revision afb9ea93adb5805b580c343bd81cc168697d1d63)
+++ lib/events_glib.c	(revision d150a9df0b0e84a3fe30af340762f96475f4b1e5)
@@ -147,2 +147,7 @@
 		g_source_remove(tag);
 }
+
+void closesocket( int fd )
+{
+	close( fd );
+}
Index: lib/misc.c
===================================================================
--- lib/misc.c	(revision afb9ea93adb5805b580c343bd81cc168697d1d63)
+++ lib/misc.c	(revision d150a9df0b0e84a3fe30af340762f96475f4b1e5)
@@ -157,4 +157,5 @@
 	char *s = out, *cs;
 	int i, matched;
+	int taglen;
 	
 	memset( out, 0, sizeof( out ) );
@@ -173,7 +174,16 @@
 				in ++;
 			
+			taglen = in - cs - 1;   /* not <0 because the above loop runs at least once */
 			if( *in )
 			{
-				if( g_strncasecmp( cs+1, "br", 2) == 0 )
+				if( g_strncasecmp( cs+1, "b", taglen) == 0 )
+					*(s++) = '\x02';
+				else if( g_strncasecmp( cs+1, "/b", taglen) == 0 )
+					*(s++) = '\x02';
+				else if( g_strncasecmp( cs+1, "i", taglen) == 0 )
+					*(s++) = '\x1f';
+				else if( g_strncasecmp( cs+1, "/i", taglen) == 0 )
+					*(s++) = '\x1f';
+				else if( g_strncasecmp( cs+1, "br", taglen) == 0 )
 					*(s++) = '\n';
 				in ++;
Index: lib/ssl_bogus.c
===================================================================
--- lib/ssl_bogus.c	(revision afb9ea93adb5805b580c343bd81cc168697d1d63)
+++ lib/ssl_bogus.c	(revision d150a9df0b0e84a3fe30af340762f96475f4b1e5)
@@ -27,4 +27,8 @@
 
 int ssl_errno;
+
+void ssl_init( void )
+{
+}
 
 void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
@@ -66,2 +70,7 @@
 	return 0;
 }
+
+int ssl_pending( void *conn )
+{
+	return 0;
+}
Index: lib/ssl_client.h
===================================================================
--- lib/ssl_client.h	(revision afb9ea93adb5805b580c343bd81cc168697d1d63)
+++ lib/ssl_client.h	(revision d150a9df0b0e84a3fe30af340762f96475f4b1e5)
@@ -47,4 +47,7 @@
 
 
+/* Perform any global initialization the SSL library might need. */
+G_MODULE_EXPORT void ssl_init( void );
+
 /* Connect to host:port, call the given function when the connection is
    ready to be used for SSL traffic. This is all done asynchronously, no
Index: lib/ssl_gnutls.c
===================================================================
--- lib/ssl_gnutls.c	(revision afb9ea93adb5805b580c343bd81cc168697d1d63)
+++ lib/ssl_gnutls.c	(revision d150a9df0b0e84a3fe30af340762f96475f4b1e5)
@@ -60,4 +60,11 @@
 static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond );
 
+
+void ssl_init( void )
+{
+	gnutls_global_init();
+	initialized = TRUE;
+	atexit( gnutls_global_deinit );
+}
 
 void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
@@ -122,7 +129,5 @@
 	if( !initialized )
 	{
-		gnutls_global_init();
-		initialized = TRUE;
-		atexit( gnutls_global_deinit );
+		ssl_init();
 	}
 	
Index: lib/ssl_nss.c
===================================================================
--- lib/ssl_nss.c	(revision afb9ea93adb5805b580c343bd81cc168697d1d63)
+++ lib/ssl_nss.c	(revision d150a9df0b0e84a3fe30af340762f96475f4b1e5)
@@ -91,4 +91,12 @@
 
 
+void ssl_init( void )
+{
+	PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
+	NSS_NoDB_Init(NULL);
+	NSS_SetDomesticPolicy();
+	initialized = TRUE;
+}
+
 void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
 {
@@ -107,7 +115,5 @@
 	if( !initialized )
 	{
-		PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
-		NSS_NoDB_Init(NULL);
-		NSS_SetDomesticPolicy();
+		ssl_init();
 	}
 
Index: lib/ssl_openssl.c
===================================================================
--- lib/ssl_openssl.c	(revision afb9ea93adb5805b580c343bd81cc168697d1d63)
+++ lib/ssl_openssl.c	(revision d150a9df0b0e84a3fe30af340762f96475f4b1e5)
@@ -56,4 +56,11 @@
 static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond );
 
+
+void ssl_init( void )
+{
+	initialized = TRUE;
+	SSL_library_init();
+	// SSLeay_add_ssl_algorithms();
+}
 
 void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
@@ -115,8 +122,5 @@
 	if( !initialized )
 	{
-		initialized = TRUE;
-		SSL_library_init();
-		//SSLeay_add_ssl_algorithms();
-		//OpenSSL_add_all_algorithms();
+		ssl_init();
 	}
 	
