diff -u -r -P -x *.orig -x *~ -x *.in pan-0.111/pan/gui/pan.cc panasync/pan/gui/pan.cc
--- pan-0.111/pan/gui/pan.cc	Tue Aug 29 17:20:44 2006
+++ panasync/pan/gui/pan.cc	Tue Sep  5 00:13:44 2006
@@ -160,6 +160,7 @@
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
   textdomain (GETTEXT_PACKAGE);
 
+  g_thread_init(NULL);
   gtk_init (&argc, &argv);
   g_mime_init (GMIME_INIT_FLAG_UTF8);
 
diff -u -r -P -x *.orig -x *~ -x *.in pan-0.111/pan/tasks/nntp-pool.cc panasync/pan/tasks/nntp-pool.cc
--- pan-0.111/pan/tasks/nntp-pool.cc	Wed Aug 30 15:28:57 2006
+++ panasync/pan/tasks/nntp-pool.cc	Tue Sep  5 02:21:29 2006
@@ -187,15 +187,10 @@
       std::string address;
       int port;
       _server_info.get_server_addr (_server, address, port);
-      Socket * sock (_socket_creator->create_socket (address, port));
-      if (sock != 0)
-      {
-         _connection_pending = true;
-         std::string user, pass;
-         _server_info.get_server_auth (_server, user, pass);
-         NNTP * nntp = new NNTP (_server, user, pass, sock);
-         nntp->handshake (this);
-      }
+ 	_connection_pending=true;
+	 std::string user, pass;
+	 _server_info.get_server_auth (_server, user, pass);
+	new NNTP(_server, user, pass, this, _socket_creator,address,port);
    }
 }
 
diff -u -r -P -x *.orig -x *~ -x *.in pan-0.111/pan/tasks/nntp.cc panasync/pan/tasks/nntp.cc
--- pan-0.111/pan/tasks/nntp.cc	Thu Aug 17 15:25:19 2006
+++ panasync/pan/tasks/nntp.cc	Mon Sep  4 02:50:57 2006
@@ -108,6 +108,23 @@
    }
 }
 
+NNTP::NNTP (const Quark        & server,
+	  const std::string  & username,
+	  const std::string  & password,
+	  Listener            * l,
+	  Socket::Creator     * creator,
+	  const std::string  & address,
+	  int                port):
+  _server(server),
+  _socket_error(false),
+  _listener(l),
+  _username(username),
+  _password(password),
+  _nntp_response_text(false)
+{
+	_socket=creator->create_socket (address, port,this);
+}
+	   
 /***
 ****  WRITING
 ***/
@@ -115,6 +132,7 @@
 void
 NNTP :: onSocketOpen (Socket * sock)
 {
+	handshake(_listener);
 }
 
 bool
diff -u -r -P -x *.orig -x *~ -x *.in pan-0.111/pan/tasks/nntp.h panasync/pan/tasks/nntp.h
--- pan-0.111/pan/tasks/nntp.h	Wed Jul 12 14:33:44 2006
+++ panasync/pan/tasks/nntp.h	Mon Sep  4 02:47:43 2006
@@ -92,21 +92,14 @@
        };
 
       public:
-
-        NNTP (const Quark        & server,
-              const std::string  & username,
-              const std::string  & password,
-              Socket         * socket):
-          _server(server),
-          _socket(socket),
-          _socket_error(false),
-          _listener(0),
-          _username(username),
-          _password(password),
-          _nntp_response_text(false)
-       {
-       }
-
+	
+	 NNTP (const Quark        & server,
+		  const std::string  & username,
+		  const std::string  & password,
+		  Listener            * l,
+		  Socket::Creator     * creator,
+		  const std::string  & address,
+		  int                port);
        virtual ~NNTP ()
        {
        }
diff -u -r -P -x *.orig -x *~ -x *.in pan-0.111/pan/tasks/socket-impl-gio.cc panasync/pan/tasks/socket-impl-gio.cc
--- pan-0.111/pan/tasks/socket-impl-gio.cc	Wed Jul 19 09:51:23 2006
+++ panasync/pan/tasks/socket-impl-gio.cc	Tue Sep  5 02:23:11 2006
@@ -38,7 +38,7 @@
 
 #ifdef G_OS_WIN32
   // this #define is necessary for mingw
-  #define _WIN32_WINNT 0x0500
+  #define _WIN32_WINNT 0x0501
   #include <ws2tcpip.h>
   #undef gai_strerror
   #define gai_strerror(i) gai_strerror_does_not_link (i)
@@ -109,7 +109,8 @@
   {
     int err;
     int sockfd;
-    Log::add_info_va (_("Making a new connection to %*.*s"), host.len, host.len, host.str);
+    // no gtk functions from seperate threads
+    //Log::add_info_va (_("Making a new connection to %*.*s"), host.len, host.len, host.str);
     ensure_module_inited ();
 
 #ifndef G_OS_WIN32
@@ -166,6 +167,7 @@
       OutputDebugString ("Connection seems to be opened");
     else {
       PrintLastError(err);
+      closesocket(sockfd);
       return NULL;
     }
 
@@ -204,34 +206,69 @@
 
     // walk through the merged list until we get a good socket
     sockfd = -1;
+    bool gotsock(false),cnct(false);
     for (l=list; l!=NULL; l=l->next)
     {
+      char hostbuf[128];
       struct addrinfo * ainfo = (struct addrinfo*) l->data;
       sockfd = ::socket (ainfo->ai_family,
                          ainfo->ai_socktype,
                          ainfo->ai_protocol);
       if (sockfd >= 0)
-        break;
+      {
+        gotsock=true;
+        if(ainfo->ai_family==AF_INET)
+        {
+          struct sockaddr_in *sin=reinterpret_cast<sockaddr_in*>(ainfo->ai_addr);
+          //struct in_addr &addr=sin->sin_addr;
+          u_char *addr=&(sin->sin_addr.s_net);
+          g_snprintf (hostbuf, sizeof(hostbuf),
+                  "Trying host %u.%u.%u.%u, port %d\n",
+                  addr[0], addr[1], addr[2], addr[3], sin->sin_port);
+        }else if(ainfo->ai_family==AF_INET6)
+        {
+          struct sockaddr_in6 *sin=reinterpret_cast<sockaddr_in6*>(ainfo->ai_addr);
+          //struct in6_addr &addr=sin->sin_addr;
+          u_short *addr=(sin->sin6_addr.s6_addr16);
+          g_snprintf (hostbuf, sizeof(hostbuf),
+                  "Trying host %x:%x:%x:%x:%x:%x:%x:%x, port %d\n",
+                  addr[0], addr[1],addr[2],addr[3],
+                  addr[4], addr[5],addr[6],addr[7],
+                  sin->sin6_port);
+        }else
+          g_snprintf (hostbuf, sizeof(hostbuf),"Trying host (unknown family).\n");
+        OutputDebugString (hostbuf);
+        // try to connect to the host
+        if (connect (sockfd, ainfo->ai_addr, ainfo->ai_addrlen) == 0)
+        {
+          cnct=true;
+          break;
+        }
+        //be nice and close the socket if it doesn't connect
+        closesocket(sockfd);
+        sockfd=-1;
+      }
     }
 
     g_list_free (list);
-    if (sockfd < 0) {
-      g_message ("couldn't create a socket.");
+    if (!gotsock) {
       ::freeaddrinfo (ans);
+      g_message ("couldn't create a socket.");
       return NULL;
     }
-
-    // try to connect to the host
-    if (connect (sockfd, ans->ai_addr, ans->ai_addrlen) < 0) {
-      g_message ("Connect failed: %s", g_strerror(errno));
+    if(!cnct)
+    {
       ::freeaddrinfo (ans);
+      g_message ("Connect failed");
       return NULL;
     }
 #endif
 
-    GIOChannel * channel = g_io_channel_unix_new (sockfd);
 #ifndef G_OS_WIN32
+    GIOChannel * channel = g_io_channel_unix_new (sockfd);
     g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
+#else
+    GIOChannel * channel = g_io_channel_win32_new_socket (sockfd);
 #endif
     if (g_io_channel_get_encoding (channel) != NULL)
       g_io_channel_set_encoding (channel, NULL, NULL);
@@ -285,18 +322,62 @@
   _in_buf = 0;
 }
 
-void
-GIOChannelSocket :: open (const StringView& address, int port, Listener * l)
+namespace {
+typedef struct
 {
-  _listener = l;
-  _channel = create_channel (address, port);
+	GIOChannelSocket *s;
+	GIOChannelSocket::Listener *l;
+	GMainContext *mc;
+	GThread *thrd;
+	std::string address;
+	int port;
+	bool opened;
+} socket_data;
 
-  if (_listener) {
-    if (_channel)
-      _listener->onSocketOpen (this);
+gboolean idlefunc(gpointer data)
+{
+	socket_data *sd=static_cast<socket_data*>(data);
+	g_thread_join(sd->thrd);
+  if (sd->l) {
+    if (sd->opened)
+      sd->l->onSocketOpen (sd->s);
     else
-      _listener->onSocketError (this);
+      sd->l->onSocketError (sd->s);
   }
+  delete sd;
+  return FALSE;
+}
+
+}
+
+gpointer GIOChannelSocket::open_thrd(gpointer data)
+{
+	socket_data *sd=static_cast<socket_data*>(data);
+	sd->thrd=g_thread_self();
+	GIOChannel *ch=create_channel(StringView(sd->address),sd->port);
+	sd->s->_channel=ch;
+	if(ch!=NULL)
+		sd->opened=true;
+	GSource *gs=g_idle_source_new();
+	g_source_set_callback(gs,idlefunc,data,NULL);
+	g_source_attach(gs,sd->mc);
+	return ch;
+}
+
+void
+GIOChannelSocket :: open (const StringView& address, int port, Listener * l)
+{
+  _listener = l;
+  socket_data *sd=new socket_data;
+  
+  sd->s=this;
+  sd->l=l;
+  sd->mc=g_main_loop_get_context(NULL);
+  sd->address=address.to_string();
+  sd->port=port;
+  sd->opened=false;
+  Log::add_info_va (_("Making a new connection to %*.*s"), address.len, address.len, address.str);
+  g_thread_create(open_thrd,sd,TRUE,NULL);
 }
 
 void
diff -u -r -P -x *.orig -x *~ -x *.in pan-0.111/pan/tasks/socket-impl-gio.h panasync/pan/tasks/socket-impl-gio.h
--- pan-0.111/pan/tasks/socket-impl-gio.h	Sun Mar 26 13:35:47 2006
+++ panasync/pan/tasks/socket-impl-gio.h	Mon Sep  4 02:39:02 2006
@@ -53,6 +53,7 @@
          static gboolean read_later (gpointer);
          bool do_read ();
          bool do_write ();
+		 static gpointer open_thrd(gpointer data);
 
       public:
 
@@ -61,9 +62,9 @@
           */
          struct Creator: public Socket::Creator {
             virtual ~Creator () { }
-            virtual Socket* create_socket (const StringView& host, int port) {
+            virtual Socket* create_socket (const StringView& host, int port, Listener *l) {
                GIOChannelSocket * sock = new GIOChannelSocket ();
-               sock->open (host, port, NULL);
+               sock->open (host, port, l);
                return sock;
             }
          };
diff -u -r -P -x *.orig -x *~ -x *.in pan-0.111/pan/tasks/socket.h panasync/pan/tasks/socket.h
--- pan-0.111/pan/tasks/socket.h	Fri May  5 01:05:48 2006
+++ panasync/pan/tasks/socket.h	Mon Sep  4 02:38:38 2006
@@ -78,7 +78,7 @@
        */
       struct Creator {
         virtual ~Creator () { }
-        virtual Socket* create_socket (const StringView& host, int port) = 0;
+        virtual Socket* create_socket (const StringView& host, int port, Listener *l) = 0;
       };
   };
 }
