Index: protocols/msn/gw.c
===================================================================
--- protocols/msn/gw.c	(revision 5fbf8152c7e760ca2c4140f54ba56c62bcd2571b)
+++ protocols/msn/gw.c	(revision 951aefdab1f09f35f2deef8c490b590313c94192)
@@ -15,5 +15,5 @@
 static gboolean msn_gw_poll_timeout(gpointer data, gint source, b_input_condition cond);
 
-struct msn_gw *msn_gw_new(struct msn_data *md)
+struct msn_gw *msn_gw_new(struct im_connection *ic)
 {
 	struct msn_gw *gw = g_new0(struct msn_gw, 1);
@@ -22,5 +22,6 @@
 	gw->ssl = (GATEWAY_PORT == 443);
 	gw->poll_timeout = -1;
-	gw->data = md;
+	gw->ic = ic;
+	gw->md = ic->proto_data;
 	gw->in = g_byte_array_new();
 	gw->out = g_byte_array_new();
@@ -38,4 +39,14 @@
 	g_free(gw->last_host);
 	g_free(gw);
+}
+
+static struct msn_gw *msn_gw_from_ic(struct im_connection *ic)
+{
+	if (g_slist_find(msn_connections, ic) == NULL) {
+		return NULL;
+	} else {
+		struct msn_data *md = ic->proto_data;
+		return md->gw;
+	}
 }
 
@@ -66,16 +77,13 @@
 void msn_gw_callback(struct http_request *req)
 {
+	struct msn_gw *gw;
 	char *value;
-	struct msn_gw *gw = req->data;
+
+	if (!(gw = msn_gw_from_ic(req->data))) {
+		return;
+	}
 
 	gw->waiting = FALSE;
 	gw->polling = FALSE;
-
-	if (!gw->open) {
-		/* the user tried to logout while the request was pending
-		 * see msn_ns_close() */
-		msn_gw_free(gw);
-		return;
-	}
 
 	if (getenv("BITLBEE_DEBUG")) {
@@ -85,5 +93,5 @@
 
 	if (req->status_code != 200) {
-		gw->callback(gw->data, -1, B_EV_IO_READ);
+		gw->callback(gw->md, -1, B_EV_IO_READ);
 		return;
 	}
@@ -91,5 +99,5 @@
 	if ((value = get_rfc822_header(req->reply_headers, "X-MSN-Messenger", 0))) {
 		if (!msn_gw_parse_session_header(gw, value)) {
-			gw->callback(gw->data, -1, B_EV_IO_READ);
+			gw->callback(gw->md, -1, B_EV_IO_READ);
 			g_free(value);
 			return;
@@ -105,5 +113,5 @@
 	if (req->body_size) {
 		g_byte_array_append(gw->in, (const guint8 *) req->reply_body, req->body_size);
-		gw->callback(gw->data, -1, B_EV_IO_READ);
+		gw->callback(gw->md, -1, B_EV_IO_READ);
 	}
 
@@ -111,5 +119,5 @@
 		b_event_remove(gw->poll_timeout);
 	}
-	gw->poll_timeout = b_timeout_add(500, msn_gw_poll_timeout, gw);
+	gw->poll_timeout = b_timeout_add(500, msn_gw_poll_timeout, gw->ic);
 
 }
@@ -136,5 +144,5 @@
 		gw->session_id ? : "", args ? : "", gw->last_host, bodylen, body ? : "");
 
-	http_dorequest(gw->last_host, gw->port, gw->ssl, request, msn_gw_callback, gw);
+	http_dorequest(gw->last_host, gw->port, gw->ssl, request, msn_gw_callback, gw->ic);
 	gw->open = TRUE;
 	gw->waiting = TRUE;
@@ -151,5 +159,10 @@
 static gboolean msn_gw_poll_timeout(gpointer data, gint source, b_input_condition cond)
 {
-	struct msn_gw *gw = data;
+	struct msn_gw *gw;
+
+	if (!(gw = msn_gw_from_ic(data))) {
+		return FALSE;
+	}
+
 	gw->poll_timeout = -1;
 	if (!gw->waiting) {
Index: protocols/msn/msn.h
===================================================================
--- protocols/msn/msn.h	(revision 5fbf8152c7e760ca2c4140f54ba56c62bcd2571b)
+++ protocols/msn/msn.h	(revision 951aefdab1f09f35f2deef8c490b590313c94192)
@@ -137,5 +137,7 @@
 
 	b_event_handler callback;
-	gpointer data;
+
+	struct im_connection *ic;
+	struct msn_data *md;
 
 	gboolean open;
@@ -265,5 +267,5 @@
 
 /* gw.c */
-struct msn_gw *msn_gw_new(struct msn_data *md);
+struct msn_gw *msn_gw_new(struct im_connection *ic);
 void msn_gw_free(struct msn_gw *gw);
 void msn_gw_open(struct msn_gw *gw);
Index: protocols/msn/ns.c
===================================================================
--- protocols/msn/ns.c	(revision 5fbf8152c7e760ca2c4140f54ba56c62bcd2571b)
+++ protocols/msn/ns.c	(revision 951aefdab1f09f35f2deef8c490b590313c94192)
@@ -90,5 +90,5 @@
 
 	if (md->is_http) {
-		md->gw = msn_gw_new(md);
+		md->gw = msn_gw_new(ic);
 		md->gw->callback = msn_ns_callback;
 		msn_ns_connected(md, -1, B_EV_IO_READ);
@@ -148,10 +148,5 @@
 {
 	if (md->gw) {
-		if (md->gw->waiting) {
-			/* mark it as closed, let the request callback clean it */
-			md->gw->open = FALSE;
-		} else {
-			msn_gw_free(md->gw);
-		}
+		msn_gw_free(md->gw);
 	}
 	if (md->fd >= 0) {
