Index: lib/proxy.c
===================================================================
--- lib/proxy.c	(revision 5ebff60479fc7a9f7f50ac03b124c91d4e6ebe11)
+++ lib/proxy.c	(revision e9face75a662224c30c9e71e07f031b4d4450f74)
@@ -63,5 +63,14 @@
 static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb);
 
-static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond)
+static gboolean phb_close(struct PHB *phb)
+{
+	close(phb->fd);
+	phb->func(phb->data, -1, B_EV_IO_READ);
+	g_free(phb->host);
+	g_free(phb);
+	return FALSE;
+}
+
+static gboolean proxy_connected(gpointer data, gint source, b_input_condition cond)
 {
 	struct PHB *phb = data;
@@ -80,22 +89,16 @@
 				dup2(new_fd, source);
 				closesocket(new_fd);
-				phb->inpa = b_input_add(source, B_EV_IO_WRITE, gaim_io_connected, phb);
+				phb->inpa = b_input_add(source, B_EV_IO_WRITE, proxy_connected, phb);
 				return FALSE;
 			}
 		}
-		freeaddrinfo(phb->gai);
 		closesocket(source);
-		b_event_remove(phb->inpa);
-		phb->inpa = 0;
-		if (phb->proxy_func) {
-			phb->proxy_func(phb->proxy_data, -1, B_EV_IO_READ);
-		} else {
-			phb->func(phb->data, -1, B_EV_IO_READ);
-			g_free(phb);
-		}
-		return FALSE;
-	}
+		source = -1;
+		/* socket is dead, but continue to clean up */
+	} else {
+		sock_make_blocking(source);
+	}
+
 	freeaddrinfo(phb->gai);
-	sock_make_blocking(source);
 	b_event_remove(phb->inpa);
 	phb->inpa = 0;
@@ -160,5 +163,5 @@
 			continue;
 		} else {
-			phb->inpa = b_input_add(fd, B_EV_IO_WRITE, gaim_io_connected, phb);
+			phb->inpa = b_input_add(fd, B_EV_IO_WRITE, proxy_connected, phb);
 			phb->fd = fd;
 
@@ -206,10 +209,5 @@
 	}
 
-	close(source);
-	phb->func(phb->data, -1, B_EV_IO_READ);
-	g_free(phb->host);
-	g_free(phb);
-
-	return FALSE;
+	return phb_close(phb);
 }
 
@@ -226,9 +224,5 @@
 	len = sizeof(error);
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 	sock_make_blocking(source);
@@ -237,9 +231,5 @@
 	           phb->host, phb->port);
 	if (send(source, cmd, strlen(cmd), 0) < 0) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
@@ -252,9 +242,5 @@
 		g_free(t2);
 		if (send(source, cmd, strlen(cmd), 0) < 0) {
-			close(source);
-			phb->func(phb->data, -1, B_EV_IO_READ);
-			g_free(phb->host);
-			g_free(phb);
-			return FALSE;
+			return phb_close(phb);
 		}
 	}
@@ -262,9 +248,5 @@
 	g_snprintf(cmd, sizeof(cmd), "\r\n");
 	if (send(source, cmd, strlen(cmd), 0) < 0) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
@@ -302,10 +284,5 @@
 	}
 
-	close(source);
-	phb->func(phb->data, -1, B_EV_IO_READ);
-	g_free(phb->host);
-	g_free(phb);
-
-	return FALSE;
+	return phb_close(phb);
 }
 
@@ -323,9 +300,5 @@
 	len = sizeof(error);
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 	sock_make_blocking(source);
@@ -333,9 +306,5 @@
 	/* XXX does socks4 not support host name lookups by the proxy? */
 	if (!(hp = gethostbyname(phb->host))) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
@@ -350,9 +319,5 @@
 	packet[8] = 0;
 	if (write(source, packet, 9) != 9) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
@@ -383,16 +348,8 @@
 
 	if (read(source, buf, 10) < 10) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 	if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
@@ -420,8 +377,5 @@
 
 	if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
+		phb_close(phb);
 		return;
 	}
@@ -438,17 +392,9 @@
 
 	if (read(source, buf, 2) < 2) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
 	if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
@@ -466,17 +412,9 @@
 
 	if (read(source, buf, 2) < 2) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
 	if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
@@ -489,9 +427,5 @@
 		memcpy(buf + 2 + i + 1, proxypass, j);
 		if (write(source, buf, 3 + i + j) < 3 + i + j) {
-			close(source);
-			phb->func(phb->data, -1, B_EV_IO_READ);
-			g_free(phb->host);
-			g_free(phb);
-			return FALSE;
+			return phb_close(phb);
 		}
 
@@ -517,9 +451,5 @@
 	len = sizeof(error);
 	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 	sock_make_blocking(source);
@@ -539,9 +469,5 @@
 
 	if (write(source, buf, i) < i) {
-		close(source);
-		phb->func(phb->data, -1, B_EV_IO_READ);
-		g_free(phb->host);
-		g_free(phb);
-		return FALSE;
+		return phb_close(phb);
 	}
 
