Index: protocols/msn/gw.c
===================================================================
--- protocols/msn/gw.c	(revision 2ca933c271083fb9fee955263c0535b6fda43b22)
+++ protocols/msn/gw.c	(revision 767b2d1ab87d2d835eec091e0aae9af6b225de83)
@@ -2,7 +2,4 @@
 #include "lib/http_client.h"
 #include "msn.h"
-
-#define GATEWAY_HOST "geo.gateway.messenger.live.com"
-#define GATEWAY_PORT 443
 
 #define REQUEST_TEMPLATE \
@@ -18,7 +15,7 @@
 {
 	struct msn_gw *gw = g_new0(struct msn_gw, 1);
-	gw->last_host = g_strdup(GATEWAY_HOST);
-	gw->port = GATEWAY_PORT;
-	gw->ssl = (GATEWAY_PORT == 443);
+	gw->last_host = g_strdup(MSN_HTTP_HOST);
+	gw->port = MSN_HTTP_PORT;
+	gw->ssl = (MSN_HTTP_PORT == 443);
 	gw->poll_timeout = -1;
 	gw->ic = ic;
Index: protocols/msn/msn.c
===================================================================
--- protocols/msn/msn.c	(revision 2ca933c271083fb9fee955263c0535b6fda43b22)
+++ protocols/msn/msn.c	(revision 767b2d1ab87d2d835eec091e0aae9af6b225de83)
@@ -43,5 +43,5 @@
 	s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
 
-	s = set_add(&acc->set, "port", MSN_NS_PORT, set_eval_int, acc);
+	s = set_add(&acc->set, "port", NULL, set_eval_int, acc);
 	s->flags |= ACC_SET_OFFLINE_ONLY;
 
@@ -60,12 +60,9 @@
 	struct im_connection *ic = imcb_new(acc);
 	struct msn_data *md = g_new0(struct msn_data, 1);
-	char *server = set_getstr(&ic->acc->set, "server");
+	char *server = set_getstr(&ic->acc->set, "server") ? : MSN_SSL_HOST;
+	int port = set_getint(&ic->acc->set, "port") ? : MSN_SSL_PORT;
 
 	ic->proto_data = md;
 	ic->flags |= OPT_PONGS | OPT_PONGED;
-
-	if (!server) {
-		server = "geo.gateway.messenger.live.com";
-	}
 
 	if (strchr(acc->user, '@') == NULL) {
@@ -84,6 +81,6 @@
 
 	imcb_log(ic, "Connecting");
-	msn_ns_connect(ic, server,
-	               set_getint(&ic->acc->set, "port"));
+
+	msn_ns_connect(ic, server, port);
 
 	if (set_getbool(&acc->set, "mail_notifications") && set_getstr(&acc->set, "mail_notifications_handle")) {
Index: protocols/msn/msn.h
===================================================================
--- protocols/msn/msn.h	(revision 2ca933c271083fb9fee955263c0535b6fda43b22)
+++ protocols/msn/msn.h	(revision 767b2d1ab87d2d835eec091e0aae9af6b225de83)
@@ -32,6 +32,9 @@
 */
 
-#define MSN_NS_HOST "messenger.hotmail.com"
-#define MSN_NS_PORT "1863"
+#define MSN_SSL_HOST "s.gateway.messenger.live.com"
+#define MSN_SSL_PORT 443
+
+#define MSN_HTTP_HOST "geo.gateway.messenger.live.com"
+#define MSN_HTTP_PORT 443
 
 /* Some other version.
@@ -154,4 +157,5 @@
 	gboolean is_http;
 	struct msn_gw *gw;
+	void *ssl;
 };
 
Index: protocols/msn/ns.c
===================================================================
--- protocols/msn/ns.c	(revision 2ca933c271083fb9fee955263c0535b6fda43b22)
+++ protocols/msn/ns.c	(revision 767b2d1ab87d2d835eec091e0aae9af6b225de83)
@@ -32,6 +32,7 @@
 #include "soap.h"
 #include "xmltree.h"
-
-static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond);
+#include "ssl_client.h"
+
+static gboolean msn_ns_connected(gpointer data, int source, void *scd, b_input_condition cond);
 static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond);
 
@@ -68,5 +69,5 @@
 		msn_gw_write(md->gw, out, len);
 	} else {
-		st = write(fd, out, len);
+		st = ssl_write(md->ssl, out, len);
 	}
 
@@ -92,7 +93,8 @@
 		md->gw = msn_gw_new(ic);
 		md->gw->callback = msn_ns_callback;
-		msn_ns_connected(md, -1, B_EV_IO_READ);
+		msn_ns_connected(md, 0, NULL, B_EV_IO_READ);
 	} else {
-		md->fd = proxy_connect(host, port, msn_ns_connected, md);
+		md->ssl = ssl_connect((char *) host, port, TRUE, msn_ns_connected, md);
+		md->fd = md->ssl ? ssl_getfd(md->ssl) : -1;
 		if (md->fd < 0) {
 			imcb_error(ic, "Could not connect to server");
@@ -105,10 +107,11 @@
 }
 
-static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond)
+static gboolean msn_ns_connected(gpointer data, int source, void *scd, b_input_condition cond)
 {
 	struct msn_data *md = data;
 	struct im_connection *ic = md->ic;
 
-	if (source == -1 && !md->is_http) {
+	if (!scd && !md->is_http) {
+		md->ssl = NULL;
 		imcb_error(ic, "Could not connect to server");
 		imc_logout(ic, TRUE);
@@ -135,5 +138,5 @@
 	}
 
-	if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) {
+	if (msn_ns_write(ic, -1, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) {
 		if (!md->is_http) {
 			md->inpa = b_input_add(md->fd, B_EV_IO_READ, msn_ns_callback, md);
@@ -150,10 +153,13 @@
 		msn_gw_free(md->gw);
 	}
-	if (md->fd >= 0) {
-		closesocket(md->fd);
+
+	if (md->ssl) {
+		ssl_disconnect(md->ssl);
 		b_event_remove(md->inpa);
 	}
 
+	md->ssl = NULL;
 	md->fd = md->inpa = -1;
+
 	g_free(md->rxq);
 	g_free(md->cmd_text);
@@ -175,8 +181,8 @@
 	} else {
 		bytes = g_malloc(1024);
-		st = read(md->fd, bytes, 1024);
-	}
-
-	if (st <= 0) {
+		st = ssl_read(md->ssl, bytes, 1024);
+	}
+
+	if (st == 0 || (st < 0 && (md->is_http || !ssl_sockerr_again(md->ssl)))) {
 		imcb_error(ic, "Error while reading from server");
 		imc_logout(ic, TRUE);
@@ -188,4 +194,8 @@
 
 	g_free(bytes);
+
+	if (!md->is_http && ssl_pending(md->ssl)) {
+		return msn_ns_callback(data, source, cond);
+	}
 
 	return msn_handler(md);
