MinGW
[oweals/gnunet.git] / src / util / connection.c
index fdf139abc80e93e1dc225975e5552b1879c210db..22a75691bead5905575b796a9edb748d8c723f8b 100644 (file)
@@ -29,9 +29,6 @@
  * 2) All EXISTING testcases pass with the new code
  * These rules should apply in general, but for this
  * module they are VERY, VERY important.
- *
- * TODO:
- * - can we integrate the nth.timeout_task with the write_task's timeout?
  */
 
 #include "platform.h"
  * Possible functions to call after connect failed or succeeded.
  */
 enum ConnectContinuations
-  {
+{
     /**
      * Call nothing.
      */
-    CC_NONE = 0,
+  COCO_NONE = 0,
 
     /**
      * Call "receive_again".
      */
-    CC_RECEIVE_AGAIN = 1,
+  COCO_RECEIVE_AGAIN = 1,
 
     /**
      * Call "transmit_ready".
      */
-    CC_TRANSMIT_READY = 2,
+  COCO_TRANSMIT_READY = 2,
 
     /**
      * Call "destroy_continuation".
      */
-    CC_DESTROY_CONTINUATION = 4
-  };
+  COCO_DESTROY_CONTINUATION = 4
+};
 
 
 /**
  * Transmission handle.  There can only be one for each connection.
- */ 
+ */
 struct GNUNET_CONNECTION_TransmitHandle
 {
 
@@ -169,7 +166,7 @@ struct GNUNET_CONNECTION_Handle
 
   /**
    * Configuration to use.
-   */ 
+   */
   const struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
@@ -247,6 +244,11 @@ struct GNUNET_CONNECTION_Handle
    */
   GNUNET_SCHEDULER_TaskIdentifier write_task;
 
+  /**
+   * Handle to a pending DNS lookup request.
+   */
+  struct GNUNET_RESOLVER_RequestHandle *dns_active;
+
   /**
    * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
    */
@@ -256,24 +258,21 @@ struct GNUNET_CONNECTION_Handle
    * Timeout for receiving (in absolute time).
    */
   struct GNUNET_TIME_Absolute receive_timeout;
-  
+
   /**
    * Functions to call after connect failed or succeeded.
    */
   enum ConnectContinuations ccs;
+
   /**
    * Maximum number of bytes to read (for receiving).
    */
   size_t max;
 
   /**
-   * Are we still waiting for DNS replies (on connect)?
-   * GNUNET_YES if we are, GNUNET_NO if we are not waiting for DNS,
-   * GNUNET_SYSERR if destroying the handle was deferred due to 
-   * a pending DNS lookup.
+   * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
    */
-  int dns_active;
+  int ignore_shutdown;
 
   /**
    * Port to connect to.
@@ -296,8 +295,9 @@ struct GNUNET_CONNECTION_Handle
  */
 struct GNUNET_CONNECTION_Handle *
 GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
-                                            *sched, struct GNUNET_NETWORK_Handle *osSocket,
-                                            size_t maxbuf)
+                                        *sched,
+                                        struct GNUNET_NETWORK_Handle
+                                        *osSocket, size_t maxbuf)
 {
   struct GNUNET_CONNECTION_Handle *ret;
   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
@@ -323,10 +323,11 @@ GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
  */
 struct GNUNET_CONNECTION_Handle *
 GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
-                                          *sched,
-                                          GNUNET_CONNECTION_AccessCheck access,
-                                          void *access_cls, struct GNUNET_NETWORK_Handle *lsock,
-                                          size_t maxbuf)
+                                      *sched,
+                                      GNUNET_CONNECTION_AccessCheck access,
+                                      void *access_cls,
+                                      struct GNUNET_NETWORK_Handle *lsock,
+                                      size_t maxbuf)
 {
   struct GNUNET_CONNECTION_Handle *ret;
   char addr[32];
@@ -339,17 +340,13 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
   void *uaddr;
 
   addrlen = sizeof (addr);
-  sock = GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
+  sock =
+    GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
   if (NULL == sock)
     {
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
       return NULL;
     }
-#ifndef MINGW
-  if (GNUNET_OK != GNUNET_NETWORK_socket_set_inheritable (sock))
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                         "fcntl");
-#endif
   if (addrlen > sizeof (addr))
     {
       GNUNET_break (0);
@@ -383,19 +380,15 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
       (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
     {
       if (aret == GNUNET_NO)
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                   _("Access denied to `%s'\n"),
-                   GNUNET_a2s(uaddr, addrlen));
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                    _("Access denied to `%s'\n"),
+                    GNUNET_a2s (uaddr, addrlen));
+      GNUNET_break (GNUNET_OK ==
+                    GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
       GNUNET_free (uaddr);
       return NULL;
     }
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             _("Accepting connection from `%s'\n"),
-             GNUNET_a2s(uaddr, addrlen));
-#endif
   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
   ret->write_buffer = (char *) &ret[1];
   ret->write_buffer_size = maxbuf;
@@ -403,6 +396,11 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
   ret->addrlen = addrlen;
   ret->sock = sock;
   ret->sched = sched;
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              _("Accepting connection from `%s': %p\n"),
+              GNUNET_a2s (uaddr, addrlen), ret);
+#endif
   return ret;
 }
 
@@ -416,7 +414,7 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
  */
 int
 GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
-                                   void **addr, size_t * addrlen)
+                               void **addr, size_t * addrlen)
 {
   if ((sock->addr == NULL) || (sock->addrlen == 0))
     return GNUNET_NO;
@@ -427,17 +425,6 @@ GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
 }
 
 
-/**
- * It is time to re-try connecting.
- * 
- * @param cls the handle for the connection that should be re-tried
- * @param tc unused scheduler taks context
- */
-static void
-retry_connect_continuation (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
 /**
  * This function is called after establishing a connection either has
  * succeeded or timed out.  Note that it is possible that the attempt
@@ -461,7 +448,86 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  */
 static void
 destroy_continuation (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext *tc);
+                      const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_CONNECTION_Handle *sock = cls;
+  GNUNET_CONNECTION_TransmitReadyNotify notify;
+  struct AddressProbe *pos;
+
+  GNUNET_assert (sock->dns_active == NULL);
+  if (0 != (sock->ccs & COCO_TRANSMIT_READY))
+    {
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Destroy waits for CCS-TR to be done (%p)\n", sock);
+#endif
+      sock->ccs |= COCO_DESTROY_CONTINUATION;
+      return;
+    }
+  if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Destroy waits for write_task to be done (%p)\n", sock);
+#endif
+      GNUNET_SCHEDULER_add_after (sock->sched,
+                                  sock->write_task,
+                                  &destroy_continuation, sock);
+      return;
+    }
+  if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
+    {
+      sock->ccs |= COCO_DESTROY_CONTINUATION;
+      return;
+    }
+  if (sock->sock != NULL)
+    {
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Shutting down socket (%p)\n", sock);
+#endif
+      GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
+    }
+  if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_SCHEDULER_add_after (sock->sched,
+                                  sock->read_task,
+                                  &destroy_continuation, sock);
+      return;
+    }
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+             "Destroy actually runs (%p)!\n", sock);
+#endif
+  if (sock->dns_active != NULL)
+    {
+      GNUNET_RESOLVER_request_cancel (sock->dns_active);
+      sock->dns_active = NULL;
+    }
+  while (NULL != (pos = sock->ap_head))
+    {
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
+      GNUNET_SCHEDULER_cancel (sock->sched, pos->task);
+      GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
+      GNUNET_free (pos);
+    }
+  GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
+  GNUNET_assert (sock->ccs == COCO_NONE);
+  if (NULL != (notify = sock->nth.notify_ready))
+    {
+      sock->nth.notify_ready = NULL;
+      notify (sock->nth.notify_ready_cls, 0, NULL);
+    }
+  if (sock->sock != NULL)
+    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
+  GNUNET_free_non_null (sock->addr);
+  GNUNET_free_non_null (sock->hostname);
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Freeing memory of connection %p.\n", sock);
+#endif
+  GNUNET_free (sock);
+}
 
 
 
@@ -487,9 +553,8 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
 {
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Failed to establish TCP connection to `%s:%u', no further addresses to try.\n",
-             h->hostname,
-             h->port);
+              "Failed to establish TCP connection to `%s:%u', no further addresses to try (%p).\n",
+              h->hostname, h->port, h);
 #endif
   /* connect failed / timed out */
   GNUNET_break (h->ap_head == NULL);
@@ -497,39 +562,45 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
   GNUNET_break (h->dns_active == GNUNET_NO);
   GNUNET_break (h->sock == NULL);
 
-  /* FIXME: trigger delayed reconnect attempt... */
   /* trigger jobs that used to wait on "connect_task" */
-  if (0 != (h->ccs & CC_RECEIVE_AGAIN))
+  if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
     {
-      h->ccs -= CC_RECEIVE_AGAIN;
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_timeout_continuation triggers receive_again (%p)\n",
+                  h);
+#endif
+      h->ccs -= COCO_RECEIVE_AGAIN;
       h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
-                                                GNUNET_NO,
-                                                GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                GNUNET_SCHEDULER_NO_TASK,
-                                                &receive_again,
-                                                h);
+                                                 GNUNET_SCHEDULER_NO_TASK,
+                                                 &receive_again, h);
     }
-  if (0 != (h->ccs & CC_TRANSMIT_READY))
+  if (0 != (h->ccs & COCO_TRANSMIT_READY))
     {
-      GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);    
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_timeout_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
+                  h);
+#endif
+      GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
       GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
       h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
-      h->ccs -= CC_TRANSMIT_READY;
+      h->ccs -= COCO_TRANSMIT_READY;
       h->write_task = GNUNET_SCHEDULER_add_after (h->sched,
-                                                 GNUNET_NO,
-                                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                 GNUNET_SCHEDULER_NO_TASK,
-                                                 &transmit_ready,
-                                                 h);
+                                                  GNUNET_SCHEDULER_NO_TASK,
+                                                  &transmit_ready, h);
     }
-  if (0 != (h->ccs & CC_DESTROY_CONTINUATION))
+  if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
     {
-      h->ccs -= CC_DESTROY_CONTINUATION;
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_timeout_continuation runs destroy_continuation (%p)\n",
+                  h);
+#endif
+      h->ccs -= COCO_DESTROY_CONTINUATION;
       GNUNET_SCHEDULER_add_continuation (h->sched,
-                                        GNUNET_NO,
-                                        &destroy_continuation,
-                                        h,
-                                        GNUNET_SCHEDULER_REASON_TIMEOUT);
+                                         &destroy_continuation,
+                                         h, GNUNET_SCHEDULER_REASON_TIMEOUT);
     }
 }
 
@@ -544,42 +615,51 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
 {
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Connection to `%s' succeeded!\n",
-             GNUNET_a2s(h->addr, h->addrlen));
+              "Connection to `%s' succeeded! (%p)\n",
+              GNUNET_a2s (h->addr, h->addrlen), h);
 #endif
   /* trigger jobs that waited for the connection */
-  if (0 != (h->ccs & CC_RECEIVE_AGAIN))
+  if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
     {
-      h->ccs -= CC_RECEIVE_AGAIN;
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_success_continuation runs receive_again (%p)\n",
+                  h);
+#endif
+      h->ccs -= COCO_RECEIVE_AGAIN;
       h->read_task = GNUNET_SCHEDULER_add_after (h->sched,
-                                                GNUNET_NO,
-                                                GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                GNUNET_SCHEDULER_NO_TASK,
-                                                &receive_again,
-                                                h);
+                                                 GNUNET_SCHEDULER_NO_TASK,
+                                                 &receive_again, h);
     }
-  if (0 != (h->ccs & CC_TRANSMIT_READY))
+  if (0 != (h->ccs & COCO_TRANSMIT_READY))
     {
-      GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);    
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
+                  h);
+#endif
+      GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
       GNUNET_SCHEDULER_cancel (h->sched, h->nth.timeout_task);
       h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
-      h->ccs -= CC_TRANSMIT_READY;
+      h->ccs -= COCO_TRANSMIT_READY;
       h->write_task =
-       GNUNET_SCHEDULER_add_write_net (h->sched,
-                                       GNUNET_NO,
-                                       GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                       GNUNET_SCHEDULER_NO_TASK,
-                                       GNUNET_TIME_absolute_get_remaining (h->nth.transmit_timeout),
-                                       h->sock, &transmit_ready, h);
+        GNUNET_SCHEDULER_add_write_net (h->sched,
+                                        GNUNET_TIME_absolute_get_remaining
+                                        (h->nth.transmit_timeout), h->sock,
+                                        &transmit_ready, h);
     }
-  if (0 != (h->ccs & CC_DESTROY_CONTINUATION))
+  if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
     {
-      h->ccs -= CC_DESTROY_CONTINUATION;
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_success_continuation runs destroy_continuation (%p)\n",
+                  h);
+#endif
+      h->ccs -= COCO_DESTROY_CONTINUATION;
       GNUNET_SCHEDULER_add_continuation (h->sched,
-                                        GNUNET_NO,
-                                        &destroy_continuation,
-                                        h,
-                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+                                         &destroy_continuation,
+                                         h,
+                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
     }
 }
 
@@ -588,12 +668,12 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
  * Scheduler let us know that we're either ready to write on the
  * socket OR connect timed out.  Do the right thing.
  *
- * @param ap the address we were probing
+ * @param cls the "struct AddressProbe*" with the address that we are probing
  * @param tc success or failure info about the connect attempt.
  */
 static void
 connect_probe_continuation (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext *tc)
+                            const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct AddressProbe *ap = cls;
   struct GNUNET_CONNECTION_Handle *h = ap->h;
@@ -605,17 +685,20 @@ connect_probe_continuation (void *cls,
   len = sizeof (error);
   errno = 0;
   error = 0;
-  if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
-       (GNUNET_OK != GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error, &len)) ||
-       (error != 0) || (errno != 0) )
+  if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
+      (GNUNET_OK !=
+       GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
+                                         &error, &len)) || (error != 0)
+      || (errno != 0))
     {
-      GNUNET_NETWORK_socket_close (ap->sock);
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
       GNUNET_free (ap);
-      if ( (NULL == h->ap_head) &&
-          (h->dns_active == GNUNET_NO) )
-       connect_fail_continuation (h);
+      if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
+        connect_fail_continuation (h);
       return;
     }
+  GNUNET_assert (h->sock == NULL);
+  GNUNET_assert (ap->sock != NULL);
   h->sock = ap->sock;
   GNUNET_assert (h->addr == NULL);
   h->addr = GNUNET_malloc (ap->addrlen);
@@ -625,7 +708,7 @@ connect_probe_continuation (void *cls,
   /* cancel all other attempts */
   while (NULL != (pos = h->ap_head))
     {
-      GNUNET_NETWORK_socket_close (pos->sock);
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
       GNUNET_SCHEDULER_cancel (h->sched, pos->task);
       GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
       GNUNET_free (pos);
@@ -634,75 +717,6 @@ connect_probe_continuation (void *cls,
 }
 
 
-/**
- * Scheduler let us know that the connect task is finished (or was
- * cancelled due to shutdown).  Now really clean up.
- *
- * @param cls our "struct GNUNET_CONNECTION_Handle *"
- * @param tc unused
- */
-static void
-destroy_continuation (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_CONNECTION_Handle *sock = cls;
-  GNUNET_CONNECTION_TransmitReadyNotify notify;
-
-  if (sock->dns_active == GNUNET_YES)
-    {
-      sock->dns_active = GNUNET_SYSERR;
-      return;
-    }
-  if (0 != (sock->ccs & CC_TRANSMIT_READY))
-    {
-      sock->ccs |= CC_DESTROY_CONTINUATION;
-      return;
-    }
-  if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
-    {
-      GNUNET_SCHEDULER_add_after (sock->sched,
-                                  GNUNET_YES,
-                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                  sock->write_task,
-                                  &destroy_continuation, sock);
-      return;
-    }
-  if (sock->sock != NULL)
-    {
-#if DEBUG_CONNECTION
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket.\n");
-#endif
-      GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
-    }
-  if (0 != (sock->ccs & CC_RECEIVE_AGAIN))
-    {
-      sock->ccs |= CC_DESTROY_CONTINUATION;
-      return;
-    }
-  if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
-    {
-      GNUNET_SCHEDULER_add_after (sock->sched,
-                                  GNUNET_YES,
-                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                  sock->read_task,
-                                  &destroy_continuation, sock);
-      return;
-    }
-  GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
-  GNUNET_assert (sock->ccs == CC_NONE);
-  if (NULL != (notify = sock->nth.notify_ready))
-    {
-      sock->nth.notify_ready = NULL;
-      notify (sock->nth.notify_ready_cls, 0, NULL);
-    }
-  if (sock->sock != NULL)
-    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
-  GNUNET_free_non_null (sock->addr);
-  GNUNET_free_non_null (sock->hostname);
-  GNUNET_free (sock);
-}
-
-
 /**
  * Try to establish a socket connection given the specified address.
  * This function is called by the resolver once we have a DNS reply.
@@ -713,40 +727,32 @@ destroy_continuation (void *cls,
  */
 static void
 try_connect_using_address (void *cls,
-                          const struct sockaddr * addr,
-                          socklen_t addrlen)
+                           const struct sockaddr *addr, socklen_t addrlen)
 {
   struct GNUNET_CONNECTION_Handle *h = cls;
   struct AddressProbe *ap;
   struct GNUNET_TIME_Relative delay;
+
   if (addr == NULL)
     {
-      if (h->dns_active == GNUNET_SYSERR)
-       {
-         h->dns_active = GNUNET_NO;
-         GNUNET_SCHEDULER_add_after (h->sched,
-                                     GNUNET_YES,
-                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                     h->read_task,
-                                     &destroy_continuation, 
-                                     h);
-         return;
-       }
-      h->dns_active = GNUNET_NO;
+      h->dns_active = NULL;
       if (NULL == h->ap_head)
-       connect_fail_continuation (h);
+        connect_fail_continuation (h);
       return;
     }
   if (h->sock != NULL)
-    return; /* already connected */
-  if (h->dns_active == GNUNET_SYSERR)
-    return; /* already destroyed */
+    return;                     /* already connected */
+  GNUNET_assert (h->addr == NULL);
   /* try to connect */
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Trying to connect using address `%s:%u/%s:%u'\n",
+              h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
+#endif
   ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
-  ap->addr = (const struct sockaddr*) &ap[1];
+  ap->addr = (const struct sockaddr *) &ap[1];
   memcpy (&ap[1], addr, addrlen);
-  ap->addrlen = addrlen;  
+  ap->addrlen = addrlen;
   ap->h = h;
 
   switch (ap->addr->sa_family)
@@ -760,37 +766,24 @@ try_connect_using_address (void *cls,
     default:
       GNUNET_break (0);
       GNUNET_free (ap);
-      return; /* not supported by us */
+      return;                   /* not supported by us */
     }
-  ap->sock = GNUNET_NETWORK_socket_socket (ap->addr->sa_family, SOCK_STREAM, 0);
+  ap->sock =
+    GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
   if (ap->sock == NULL)
     {
       GNUNET_free (ap);
-      return; /* not supported by OS */
-    }
-#ifndef MINGW
-  if (GNUNET_OK != GNUNET_NETWORK_socket_set_inheritable (ap->sock))
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                        "GNUNET_NETWORK_socket_set_inheritable");
-#endif
-  if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (ap->sock, GNUNET_NO))
-    {
-      /* we might want to treat this one as fatal... */
-      GNUNET_break (0);
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
-      GNUNET_free (ap);
-      return; 
+      return;                   /* not supported by OS */
     }
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             _("Trying to connect to `%s'\n"),
-             GNUNET_a2s(ap->addr,
-                        ap->addrlen));
+              _("Trying to connect to `%s' (%p)\n"),
+              GNUNET_a2s (ap->addr, ap->addrlen), h);
 #endif
-  if ( (GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
-                                                   ap->addr,
-                                                   ap->addrlen)) && 
-       (errno != EINPROGRESS))
+  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (ap->sock,
+                                                   ap->addr,
+                                                   ap->addrlen)) &&
+      (errno != EINPROGRESS))
     {
       /* maybe refused / unsupported address, try next */
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
@@ -802,41 +795,15 @@ try_connect_using_address (void *cls,
   delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
   if (h->nth.notify_ready != NULL)
     delay = GNUNET_TIME_relative_min (delay,
-                                     GNUNET_TIME_absolute_get_remaining (h->nth.transmit_timeout));
+                                      GNUNET_TIME_absolute_get_remaining
+                                      (h->nth.transmit_timeout));
   if (h->receiver != NULL)
     delay = GNUNET_TIME_relative_min (delay,
-                                     GNUNET_TIME_absolute_get_remaining (h->receive_timeout));
-   ap->task = GNUNET_SCHEDULER_add_write_net (h->sched,
-                                            GNUNET_NO,
-                                            GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                            GNUNET_SCHEDULER_NO_TASK,
-                                            delay,
-                                            ap->sock,
-                                            &connect_probe_continuation,
-                                            ap);
-}
-
-
-/**
- * It is time to re-try connecting.
- * 
- * @param cls the handle for the connection that should be re-tried
- * @param tc unused scheduler taks context
- */
-static void
-retry_connect_continuation (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_CONNECTION_Handle *sock = cls;
-
-  sock->dns_active = GNUNET_YES;
-  GNUNET_RESOLVER_ip_get (sock->sched,
-                         sock->cfg,
-                         sock->hostname,
-                         AF_UNSPEC,
-                         GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
-                         &try_connect_using_address,
-                         sock);
+                                      GNUNET_TIME_absolute_get_remaining
+                                      (h->receive_timeout));
+  ap->task =
+    GNUNET_SCHEDULER_add_write_net (h->sched, delay, ap->sock,
+                                    &connect_probe_continuation, ap);
 }
 
 
@@ -854,14 +821,15 @@ retry_connect_continuation (void *cls,
  * @return the socket handle
  */
 struct GNUNET_CONNECTION_Handle *
-GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched, 
-                                      const struct GNUNET_CONFIGURATION_Handle *cfg,
-                                      const char *hostname,
-                                      uint16_t port, size_t maxbuf)
+GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
+                                       const struct
+                                       GNUNET_CONFIGURATION_Handle *cfg,
+                                       const char *hostname, uint16_t port,
+                                       size_t maxbuf)
 {
   struct GNUNET_CONNECTION_Handle *ret;
 
-  GNUNET_assert (0 < strlen (hostname)); /* sanity check */
+  GNUNET_assert (0 < strlen (hostname));        /* sanity check */
   ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
   ret->cfg = cfg;
   ret->sched = sched;
@@ -869,7 +837,12 @@ GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
   ret->write_buffer_size = maxbuf;
   ret->port = port;
   ret->hostname = GNUNET_strdup (hostname);
-  retry_connect_continuation (ret, NULL);
+  ret->dns_active = GNUNET_RESOLVER_ip_get (sched,
+                                            cfg,
+                                            ret->hostname,
+                                            AF_UNSPEC,
+                                            GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
+                                            &try_connect_using_address, ret);
   return ret;
 }
 
@@ -889,40 +862,22 @@ GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle *sched,
  */
 struct GNUNET_CONNECTION_Handle *
 GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
-                                            *sched, int af_family,
-                                            const struct sockaddr *serv_addr,
-                                            socklen_t addrlen, size_t maxbuf)
+                                        *sched, int af_family,
+                                        const struct sockaddr *serv_addr,
+                                        socklen_t addrlen, size_t maxbuf)
 {
   struct GNUNET_NETWORK_Handle *s;
   struct GNUNET_CONNECTION_Handle *ret;
 
-  s = GNUNET_NETWORK_socket_socket (af_family, SOCK_STREAM, 0);
+  s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
   if (s == NULL)
     {
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING |
                            GNUNET_ERROR_TYPE_BULK, "socket");
       return NULL;
     }
-#ifndef MINGW
-#if 0
-  // FIXME NILS
-  if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                         "fcntl");
-#endif
-#endif
-  if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (s, GNUNET_NO))
-    {
-      /* we'll treat this one as fatal */
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
-      return NULL;
-    }
-#if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             _("Trying to connect to `%s'\n"),
-             GNUNET_a2s(serv_addr, addrlen));
-#endif
-  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) && (errno != EINPROGRESS))
+  if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen))
+      && (errno != EINPROGRESS))
     {
       /* maybe refused / unsupported address, try next */
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
@@ -933,6 +888,11 @@ GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
   ret->addr = GNUNET_malloc (addrlen);
   memcpy (ret->addr, serv_addr, addrlen);
   ret->addrlen = addrlen;
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              _("Trying to connect to `%s' (%p)\n"),
+              GNUNET_a2s (serv_addr, addrlen), ret);
+#endif
   return ret;
 }
 
@@ -948,8 +908,7 @@ GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
 int
 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
 {
-  if ( (sock->ap_head != NULL) ||
-       (sock->dns_active == GNUNET_YES) )
+  if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
     return GNUNET_YES;          /* still trying to connect */
   return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
 }
@@ -957,27 +916,39 @@ GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
 
 /**
  * Close the socket and free associated resources. Pending
- * transmissions are simply dropped.  A pending receive call will be
- * called with an error code of "EPIPE".
+ * transmissions may be completed or dropped depending on the
+ * arguments.   If a receive call is pending and should 
+ * NOT be completed, 'GNUNET_CONNECTION_receive_cancel'
+ * should be called explicitly first.
  *
  * @param sock socket to destroy
+ * @param finish_pending_write should pending writes be completed or aborted?
+ *        (this applies to transmissions where the data has already been
+ *        read from the application; all other transmissions should be
+ *        aborted using 'GNUNET_CONNECTION_notify_transmit_ready_cancel').
  */
 void
-GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock)
+GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
+                          int finish_pending_write)
 {
-  if ( (sock->write_buffer_off == 0) &&
-       (sock->dns_active == GNUNET_YES) )
+  if (GNUNET_NO == finish_pending_write)
     {
-      sock->dns_active = GNUNET_SYSERR;        /* if we're still trying to connect and have
-                                                 no message pending, stop trying! */
-      return;
+      if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
+       {
+         GNUNET_SCHEDULER_cancel (sock->sched,
+                                  sock->write_task);
+         sock->write_task = GNUNET_SCHEDULER_NO_TASK;
+         sock->write_buffer_off = 0;
+       }
+    }
+  if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
+    {
+      GNUNET_RESOLVER_request_cancel (sock->dns_active);
+      sock->dns_active = NULL;
     }
   GNUNET_assert (sock->sched != NULL);
-  GNUNET_SCHEDULER_add_after (sock->sched,
-                              GNUNET_YES,
-                              GNUNET_SCHEDULER_PRIORITY_KEEP,
-                              GNUNET_SCHEDULER_NO_TASK,
-                              &destroy_continuation, sock);
+  GNUNET_SCHEDULER_add_now (sock->sched,
+                           &destroy_continuation, sock);
 }
 
 
@@ -991,7 +962,7 @@ signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
 
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Network signals time out to receiver!\n");
+              "Network signals time out to receiver (%p)!\n", sh);
 #endif
   GNUNET_assert (NULL != (receiver = sh->receiver));
   sh->receiver = NULL;
@@ -1026,17 +997,33 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_CONNECTION_Receiver receiver;
 
   sh->read_task = GNUNET_SCHEDULER_NO_TASK;
+  if ( (GNUNET_YES == sh->ignore_shutdown) &&
+       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 
+    {
+      /* ignore shutdown request, go again immediately */
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Ignoring shutdown signal per configuration\n");
+#endif
+      sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
+                                                    GNUNET_TIME_absolute_get_remaining
+                                                    (sh->receive_timeout),
+                                                    sh->sock,
+                                                    &receive_ready, sh);
+      return;
+    }
   now = GNUNET_TIME_absolute_get ();
-  if ( (now.value > sh->receive_timeout.value) ||
-       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
-       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) )
+  if ((now.value > sh->receive_timeout.value) ||
+      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
+      (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
     {
 #if DEBUG_CONNECTION
       if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                   "Receive from `%s' encounters error: time out by %llums...\n",
-                   GNUNET_a2s (sh->addr, sh->addrlen),
-                   GNUNET_TIME_absolute_get_duration (sh->receive_timeout).value);
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                    "Receive from `%s' encounters error: time out by %llums... (%p)\n",
+                    GNUNET_a2s (sh->addr, sh->addrlen),
+                    GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
+                    value, sh);
 #endif
       signal_timeout (sh);
       return;
@@ -1046,21 +1033,14 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       /* connect failed for good */
 #if DEBUG_CONNECTION
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Receive encounters error, socket closed...\n");
+                  "Receive encounters error, socket closed... (%p)\n", sh);
 #endif
       signal_error (sh, ECONNREFUSED);
       return;
     }
   GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
 RETRY:
-  ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max,
-#ifndef MINGW
-      // FIXME MINGW
-      MSG_DONTWAIT
-#else
-      0
-#endif
-      );
+  ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
   if (ret == -1)
     {
       if (errno == EINTR)
@@ -1074,10 +1054,9 @@ RETRY:
     }
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "receive_ready read %u/%u bytes from `%s'!\n",
-             (unsigned int) ret,
-             sh->max,
-             GNUNET_a2s(sh->addr, sh->addrlen));
+              "receive_ready read %u/%u bytes from `%s' (%p)!\n",
+              (unsigned int) ret,
+              sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
 #endif
   GNUNET_assert (NULL != (receiver = sh->receiver));
   sh->receiver = NULL;
@@ -1102,12 +1081,12 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GNUNET_TIME_Absolute now;
 
   sh->read_task = GNUNET_SCHEDULER_NO_TASK;
-  if (sh->sock == NULL) 
+  if (sh->sock == NULL)
     {
       /* not connected and no longer trying */
 #if DEBUG_CONNECTION
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Receive encounters error, socket closed...\n");
+                  "Receive encounters error, socket closed (%p)...\n", sh);
 #endif
       signal_error (sh, ECONNREFUSED);
       return;
@@ -1118,7 +1097,7 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     {
 #if DEBUG_CONNECTION
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Receive encounters error: time out...\n");
+                  "Receive encounters error: time out (%p)...\n", sh);
 #endif
       signal_timeout (sh);
       return;
@@ -1126,14 +1105,10 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_assert (sh->sock != NULL);
   /* connect succeeded, wait for data! */
   sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched,
-                                                GNUNET_YES,
-                                                GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                GNUNET_SCHEDULER_NO_TASK,
-                                                GNUNET_TIME_absolute_get_remaining
-                                                (sh->receive_timeout),
-                                                sh->sock,
-                                                &receive_ready,
-                                                sh);
+                                                 GNUNET_TIME_absolute_get_remaining
+                                                 (sh->receive_timeout),
+                                                 sh->sock,
+                                                 &receive_ready, sh);
 }
 
 
@@ -1144,7 +1119,6 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * receive call per socket at any given point in time (so do not
  * call receive again until the receiver callback has been invoked).
  *
- * @param sched scheduler to use
  * @param sock socket handle
  * @param max maximum number of bytes to read
  * @param timeout maximum amount of time to wait (use -1 for "forever")
@@ -1153,14 +1127,15 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  */
 void
 GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
-                        size_t max,
-                        struct GNUNET_TIME_Relative timeout,
-                        GNUNET_CONNECTION_Receiver receiver, void *receiver_cls)
+                           size_t max,
+                           struct GNUNET_TIME_Relative timeout,
+                           GNUNET_CONNECTION_Receiver receiver,
+                           void *receiver_cls)
 {
   struct GNUNET_SCHEDULER_TaskContext tc;
 
   GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
-                (0 == (sock->ccs & CC_RECEIVE_AGAIN)) &&
+                 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
                  (sock->receiver == NULL));
   sock->receiver = receiver;
   sock->receiver_cls = receiver_cls;
@@ -1174,13 +1149,26 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
       receive_again (sock, &tc);
       return;
     }
-  if ( (sock->dns_active != GNUNET_YES) &&
-       (sock->ap_head == NULL) )
+  if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
     {
       receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
       return;
     }
-  sock->ccs += CC_RECEIVE_AGAIN;
+  sock->ccs += COCO_RECEIVE_AGAIN;
+}
+
+
+/**
+ * Configure this connection to ignore shutdown signals.
+ *
+ * @param sock socket handle
+ * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
+ */
+void
+GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
+                                  int do_ignore)
+{
+  sock->ignore_shutdown = do_ignore;
 }
 
 
@@ -1197,14 +1185,14 @@ GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
 {
   if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
     {
-      GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched, 
-                                                     sock->read_task));
+      GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched,
+                                                      sock->read_task));
       sock->read_task = GNUNET_SCHEDULER_NO_TASK;
     }
   else
-    {                                         
-      GNUNET_assert (0 != (sock->ccs & CC_RECEIVE_AGAIN));
-      sock->ccs -= CC_RECEIVE_AGAIN;
+    {
+      GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
+      sock->ccs -= COCO_RECEIVE_AGAIN;
     }
   sock->receiver = NULL;
   return sock->receiver_cls;
@@ -1261,22 +1249,48 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
  * This task notifies the client about the timeout.
  */
 static void
-transmit_timeout (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext *tc)
+transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CONNECTION_Handle *sock = cls;
   GNUNET_CONNECTION_TransmitReadyNotify notify;
 
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "transmit_timeout running (%p)\n", sock);
+#endif
   sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
 #if DEBUG_CONNECTION
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-             "Transmit to `%s:%u/%s' fails, time out reached.\n",
-             sock->hostname,
-             sock->port,
-             GNUNET_a2s (sock->addr, sock->addrlen));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
+              sock->hostname,
+              sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
 #endif
-  GNUNET_assert (0 != (sock->ccs & CC_TRANSMIT_READY));
-  sock->ccs -= CC_TRANSMIT_READY; /* remove request */
+  GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
+  sock->ccs -= COCO_TRANSMIT_READY;     /* remove request */
+  notify = sock->nth.notify_ready;
+  sock->nth.notify_ready = NULL;
+  notify (sock->nth.notify_ready_cls, 0, NULL);
+}
+
+
+/**
+ * Task invoked by the scheduler when we failed to connect
+ * at the time of being asked to transmit.
+ *
+ * This task notifies the client about the error.
+ */
+static void
+connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_CONNECTION_Handle *sock = cls;
+  GNUNET_CONNECTION_TransmitReadyNotify notify;
+
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Transmission request of size %u fails, connection failed (%p).\n",
+              sock->nth.notify_size, sock);
+#endif
+  sock->write_task = GNUNET_SCHEDULER_NO_TASK;
   notify = sock->nth.notify_ready;
   sock->nth.notify_ready = NULL;
   notify (sock->nth.notify_ready_cls, 0, NULL);
@@ -1312,14 +1326,33 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   ssize_t ret;
   size_t have;
 
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "transmit_ready running (%p).\n", sock);
+#endif
   GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
   sock->write_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 
+    {
+      if (sock->ignore_shutdown == GNUNET_YES)
+       goto SCHEDULE_WRITE;    /* ignore shutdown, go again immediately */
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Transmit to `%s' fails, shutdown happened (%p).\n",
+                  GNUNET_a2s (sock->addr, sock->addrlen), sock);
+#endif
+      notify = sock->nth.notify_ready;
+      sock->nth.notify_ready = NULL;
+      notify (sock->nth.notify_ready_cls, 0, NULL);
+      return;
+    }
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
     {
 #if DEBUG_CONNECTION
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-                 "Transmit to `%s' fails, time out reached.\n",
-                 GNUNET_a2s (sock->addr, sock->addrlen));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Transmit to `%s' fails, time out reached (%p).\n",
+                  GNUNET_a2s (sock->addr, sock->addrlen), sock);
 #endif
       notify = sock->nth.notify_ready;
       sock->nth.notify_ready = NULL;
@@ -1327,48 +1360,47 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       return;
     }
   GNUNET_assert (NULL != sock->sock);
-  if (! GNUNET_NETWORK_fdset_isset (tc->write_ready, 
-                                   sock->sock))
+  if (tc->write_ready == NULL)
+    {
+      /* special circumstances (in particular,
+         PREREQ_DONE after connect): not yet ready to write,
+         but no "fatal" error either.  Hence retry.  */
+      goto SCHEDULE_WRITE;
+    }
+  if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
     {
 #if DEBUG_CONNECTION
       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  _("Could not satisfy pending transmission request, socket closed or connect failed.\n"));
+                  _
+                  ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
+                  sock);
 #endif
       if (NULL != sock->sock)
-       {
-         GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
-         GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
-         sock->sock = NULL;
-       }
+        {
+          GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
+          GNUNET_break (GNUNET_OK ==
+                        GNUNET_NETWORK_socket_close (sock->sock));
+          sock->sock = NULL;
+        }
       transmit_error (sock);
       return;                   /* connect failed for good, we're finished */
     }
- if ((tc->write_ready == NULL) || (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock)))
-    {
-      /* special circumstances (in particular,
-        PREREQ_DONE after connect): not yet ready to write,
-        but no "fatal" error either.  Hence retry.  */
-      goto SCHEDULE_WRITE;
-    }
   GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
   process_notify (sock);
   have = sock->write_buffer_off - sock->write_buffer_pos;
   if (have == 0)
     {
-       /* no data ready for writing, terminate write loop */
+      /* no data ready for writing, terminate write loop */
       return;
     }
+  GNUNET_assert (have <= sock->write_buffer_size);
+  GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
+  GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
 RETRY:
   ret = GNUNET_NETWORK_socket_send (sock->sock,
-                                   &sock->write_buffer[sock->write_buffer_pos],
-                                   have,
-#ifndef MINGW
-              // FIXME NILS
-              MSG_DONTWAIT | MSG_NOSIGNAL
-#else
-              0
-#endif
-  );
+                                    &sock->write_buffer[sock->
+                                                        write_buffer_pos],
+                                    have);
   if (ret == -1)
     {
       if (errno == EINTR)
@@ -1376,18 +1408,15 @@ RETRY:
 #if DEBUG_CONNECTION
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
 #endif
-      GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
-      sock->sock = NULL;
+      GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_WR);
       transmit_error (sock);
       return;
     }
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "transmit_ready transmitted %u/%u bytes to `%s'\n",
-             (unsigned int) ret,
-             have,
-             GNUNET_a2s(sock->addr, sock->addrlen));
+              "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
+              (unsigned int) ret,
+              have, GNUNET_a2s (sock->addr, sock->addrlen), sock);
 #endif
   sock->write_buffer_pos += ret;
   if (sock->write_buffer_pos == sock->write_buffer_off)
@@ -1396,20 +1425,20 @@ RETRY:
       sock->write_buffer_pos = 0;
       sock->write_buffer_off = 0;
     }
-  if ( (sock->write_buffer_off == 0) && 
-       (NULL == sock->nth.notify_ready))
-    return;                     /* all data sent! */    
+  if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
+    return;                     /* all data sent! */
   /* not done writing, schedule more */
 SCHEDULE_WRITE:
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
+#endif
   if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
     sock->write_task =
       GNUNET_SCHEDULER_add_write_net (tc->sched,
-                                     GNUNET_NO,
-                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                     GNUNET_SCHEDULER_NO_TASK,
-                                     GNUNET_TIME_absolute_get_remaining (sock->nth.transmit_timeout),
-                                     sock->sock, 
-                                     &transmit_ready, sock);
+                                      GNUNET_TIME_absolute_get_remaining
+                                      (sock->nth.transmit_timeout),
+                                      sock->sock, &transmit_ready, sock);
 }
 
 
@@ -1429,28 +1458,15 @@ SCHEDULE_WRITE:
  */
 struct GNUNET_CONNECTION_TransmitHandle *
 GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
-                                      *sock, size_t size,
-                                      struct GNUNET_TIME_Relative timeout,
-                                      GNUNET_CONNECTION_TransmitReadyNotify
-                                      notify, void *notify_cls)
+                                         *sock, size_t size,
+                                         struct GNUNET_TIME_Relative timeout,
+                                         GNUNET_CONNECTION_TransmitReadyNotify
+                                         notify, void *notify_cls)
 {
   if (sock->nth.notify_ready != NULL)
     return NULL;
   GNUNET_assert (notify != NULL);
   GNUNET_assert (sock->write_buffer_size >= size);
-
-  if ( (sock->sock == NULL) &&
-       (sock->ap_head == NULL) &&
-       (sock->dns_active != GNUNET_YES) )
-    {
-#if DEBUG_CONNECTION
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Transmission request of size %u fails, connection failed.\n",
-                 size);
-#endif
-      notify (notify_cls, 0, NULL);
-      return &sock->nth;
-    }
   GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
   GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off);
@@ -1460,27 +1476,41 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
   sock->nth.notify_size = size;
   sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
-  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task);
+  if ((sock->sock == NULL) &&
+      (sock->ap_head == NULL) && (sock->dns_active == NULL))
+    {
+      sock->write_task = GNUNET_SCHEDULER_add_now (sock->sched,
+                                                  &connect_error, sock);
+      return &sock->nth;
+    }
+  if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
+    return &sock->nth;
   if (sock->sock != NULL)
     {
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Scheduling transmit_ready (%p).\n", sock);
+#endif
       sock->write_task = GNUNET_SCHEDULER_add_write_net (sock->sched,
-                                                        GNUNET_NO,
-                                                        GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                        GNUNET_SCHEDULER_NO_TASK,
-                                                        GNUNET_TIME_absolute_get_remaining (sock->nth.transmit_timeout),
-                                                        sock->sock,
-                                                        &transmit_ready, sock);
+                                                         GNUNET_TIME_absolute_get_remaining
+                                                         (sock->nth.
+                                                          transmit_timeout),
+                                                         sock->sock,
+                                                         &transmit_ready,
+                                                         sock);
     }
   else
     {
-      sock->ccs |= CC_TRANSMIT_READY;   
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
+                  sock);
+#endif
+      sock->ccs |= COCO_TRANSMIT_READY;
       sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
-                                                            GNUNET_NO,
-                                                            GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                            GNUNET_SCHEDULER_NO_TASK,
-                                                            timeout,
-                                                            &transmit_timeout,
-                                                            sock);
+                                                             timeout,
+                                                             &transmit_timeout,
+                                                             sock);
     }
   return &sock->nth;
 }
@@ -1492,30 +1522,30 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
  */
 void
 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
-                                               GNUNET_CONNECTION_TransmitHandle *h)
+                                                GNUNET_CONNECTION_TransmitHandle
+                                                *h)
 {
   GNUNET_assert (h->notify_ready != NULL);
-  if (0 != (h->sh->ccs & CC_TRANSMIT_READY))
+  if (0 != (h->sh->ccs & COCO_TRANSMIT_READY))
     {
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "notify_transmit_ready_cancel cancels timeout_task (%p)\n",
+                  h);
+#endif
       GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
       h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-      h->sh->ccs -= CC_TRANSMIT_READY;
+      h->sh->ccs -= COCO_TRANSMIT_READY;
     }
   else
     {
-      GNUNET_SCHEDULER_cancel (h->sh->sched,
-                              h->sh->write_task);
-      h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
+      if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
+       {
+         GNUNET_SCHEDULER_cancel (h->sh->sched, h->sh->write_task);
+         h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
+       }
     }
   h->notify_ready = NULL;
 }
 
-
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
-#endif
-#ifdef __cplusplus
-}
-#endif
-
 /* end of connection.c */