LRN: Fix automake deps to allow -j* builds again
[oweals/gnunet.git] / src / util / connection.c
index fa6542325db3fcafb78fcdee08ea54b1b5d29d3d..f26130a0689d4ffdbb161f299952e6da4fc6eb5c 100644 (file)
  * 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 merge receive_ready and receive_again?
- * - can we integrate the nth.timeout_task with the write_task's timeout?
  */
 
 #include "platform.h"
 #include "gnunet_common.h"
 #include "gnunet_connection_lib.h"
+#include "gnunet_container_lib.h"
+#include "gnunet_resolver_service.h"
 #include "gnunet_scheduler_lib.h"
+#include "gnunet_server_lib.h"
 
 #define DEBUG_CONNECTION GNUNET_NO
 
 /**
- * List of address families to give as hints to
- * getaddrinfo, in reverse order of  preference.
+ * Possible functions to call after connect failed or succeeded.
  */
-static int address_families[] = 
-  { AF_INET, AF_INET6, AF_UNSPEC };
+enum ConnectContinuations
+{
+    /**
+     * Call nothing.
+     */
+  COCO_NONE = 0,
+
+    /**
+     * Call "receive_again".
+     */
+  COCO_RECEIVE_AGAIN = 1,
+
+    /**
+     * Call "transmit_ready".
+     */
+  COCO_TRANSMIT_READY = 2,
+
+    /**
+     * Call "destroy_continuation".
+     */
+  COCO_DESTROY_CONTINUATION = 4
+};
+
 
+/**
+ * Transmission handle.  There can only be one for each connection.
+ */
 struct GNUNET_CONNECTION_TransmitHandle
 {
 
@@ -86,6 +108,51 @@ struct GNUNET_CONNECTION_TransmitHandle
 
 };
 
+
+/**
+ * During connect, we try multiple possible IP addresses
+ * to find out which one might work. 
+ */
+struct AddressProbe
+{
+
+  /**
+   * This is a linked list.
+   */
+  struct AddressProbe *next;
+
+  /**
+   * This is a doubly-linked list.
+   */
+  struct AddressProbe *prev;
+
+  /**
+   * The address; do not free (allocated at the end of this struct).
+   */
+  const struct sockaddr *addr;
+
+  /**
+   * Underlying OS's socket.
+   */
+  struct GNUNET_NETWORK_Handle *sock;
+
+  /**
+   * Connection for which we are probing.
+   */
+  struct GNUNET_CONNECTION_Handle *h;
+
+  /**
+   * Lenth of addr.
+   */
+  socklen_t addrlen;
+
+  /**
+   * Task waiting for the socket to finish connecting.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier task;
+};
+
+
 /**
  * @brief handle for a network socket
  */
@@ -93,19 +160,21 @@ struct GNUNET_CONNECTION_Handle
 {
 
   /**
-   * Scheduler that was used for the connect task.
+   * Configuration to use.
    */
-  struct GNUNET_SCHEDULER_Handle *sched;
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
 
   /**
-   * Address information for connect (may be NULL).
+   * Linked list of sockets we are currently trying out 
+   * (during connect).
    */
-  struct addrinfo *ai;
+  struct AddressProbe *ap_head;
 
   /**
-   * Index for the next struct addrinfo for connect attempts (may be NULL)
+   * Linked list of sockets we are currently trying out 
+   * (during connect).
    */
-  struct addrinfo *ai_pos;
+  struct AddressProbe *ap_tail;
 
   /**
    * Network address of the other end-point, may be NULL.
@@ -118,13 +187,28 @@ struct GNUNET_CONNECTION_Handle
    */
   char *hostname;
 
+  /**
+   * Underlying OS's socket, set to NULL after fatal errors.
+   */
+  struct GNUNET_NETWORK_Handle *sock;
+
+  /**
+   * Function to call on data received, NULL if no receive is pending.
+   */
+  GNUNET_CONNECTION_Receiver receiver;
+
+  /**
+   * Closure for receiver.
+   */
+  void *receiver_cls;
+
   /**
    * Pointer to our write buffer.
    */
   char *write_buffer;
 
   /**
-   * Size of our write buffer.
+   * Current size of our write buffer.
    */
   size_t write_buffer_size;
 
@@ -136,7 +220,7 @@ struct GNUNET_CONNECTION_Handle
 
   /**
    * Current read-offset in write buffer (how many
-   * bytes have already been send).
+   * bytes have already been sent).
    */
   size_t write_buffer_pos;
 
@@ -146,25 +230,24 @@ struct GNUNET_CONNECTION_Handle
   socklen_t addrlen;
 
   /**
-   * Offset in our address family list
-   * that we used last.
+   * Read task that we may need to wait for.
    */
-  int af_fam_offset;
+  GNUNET_SCHEDULER_TaskIdentifier read_task;
 
   /**
-   * Connect task that we may need to wait for.
+   * Write task that we may need to wait for.
    */
-  GNUNET_SCHEDULER_TaskIdentifier connect_task;
+  GNUNET_SCHEDULER_TaskIdentifier write_task;
 
   /**
-   * Read task that we may need to wait for.
+   * Destroy task (if already scheduled).
    */
-  GNUNET_SCHEDULER_TaskIdentifier read_task;
+  GNUNET_SCHEDULER_TaskIdentifier destroy_task;
 
   /**
-   * Write task that we may need to wait for.
+   * Handle to a pending DNS lookup request.
    */
-  GNUNET_SCHEDULER_TaskIdentifier write_task;
+  struct GNUNET_RESOLVER_RequestHandle *dns_active;
 
   /**
    * The handle we return for GNUNET_CONNECTION_notify_transmit_ready.
@@ -172,62 +255,86 @@ struct GNUNET_CONNECTION_Handle
   struct GNUNET_CONNECTION_TransmitHandle nth;
 
   /**
-   * Underlying OS's socket, set to NULL after fatal errors.
+   * Timeout for receiving (in absolute time).
    */
-  struct GNUNET_NETWORK_Handle *sock;
+  struct GNUNET_TIME_Absolute receive_timeout;
 
   /**
-   * Port to connect to.
+   * Functions to call after connect failed or succeeded.
    */
-  uint16_t port;
+  enum ConnectContinuations ccs;
 
   /**
-   * Function to call on data received, NULL
-   * if no receive is pending.
+   * Maximum number of bytes to read (for receiving).
    */
-  GNUNET_CONNECTION_Receiver receiver;
+  size_t max;
 
   /**
-   * Closure for receiver.
+   * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket.
    */
-  void *receiver_cls;
+  int ignore_shutdown;
 
   /**
-   * Timeout for receiving (in absolute time).
+   * Port to connect to.
    */
-  struct GNUNET_TIME_Absolute receive_timeout;
+  uint16_t port;
 
   /**
-   * Maximum number of bytes to read
-   * (for receiving).
+   * When shutdown, do not ever actually close the socket, but
+   * free resources.  Only should ever be set if using program
+   * termination as a signal (because only then will the leaked
+   * socket be freed!)
    */
-  size_t max;
+  int16_t persist;
 
 };
 
+/**
+ * Set the persist option on this connection handle.  Indicates
+ * that the underlying socket or fd should never really be closed.
+ * Used for indicating process death.
+ *
+ * @param sock the connection to set persistent
+ */
+void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *sock)
+{
+  sock->persist = GNUNET_YES;
+}
+
+
+/**
+ * Disable the "CORK" feature for communication with the given socket,
+ * forcing the OS to immediately flush the buffer on transmission
+ * instead of potentially buffering multiple messages.  Essentially
+ * reduces the OS send buffers to zero.
+ * Used to make sure that the last messages sent through the connection
+ * reach the other side before the process is terminated.
+ *
+ * @param sock the connection to make flushing and blocking
+ * @return GNUNET_OK on success
+ */
+int GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
+{
+  return GNUNET_NETWORK_socket_disable_corking (sock->sock);
+}
 
 /**
  * Create a socket handle by boxing an existing OS socket.  The OS
  * socket should henceforth be no longer used directly.
  * GNUNET_socket_destroy will close it.
  *
- * @param sched scheduler to use
  * @param osSocket existing socket to box
- * @param maxbuf maximum write buffer size for the socket (use
- *        0 for sockets that need no write buffers, such as listen sockets)
  * @return the boxed socket handle
  */
 struct GNUNET_CONNECTION_Handle *
-GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
-                                            *sched, struct GNUNET_NETWORK_Handle *osSocket,
-                                            size_t maxbuf)
+GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle
+                                        *osSocket)
 {
   struct GNUNET_CONNECTION_Handle *ret;
-  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
-  ret->write_buffer = (char *) &ret[1];
-  ret->write_buffer_size = maxbuf;
+  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
+  ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
+  ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
   ret->sock = osSocket;
-  ret->sched = sched;
   return ret;
 }
 
@@ -236,23 +343,18 @@ GNUNET_CONNECTION_create_from_existing (struct GNUNET_SCHEDULER_Handle
  * Create a socket handle by accepting on a listen socket.  This
  * function may block if the listen socket has no connection ready.
  *
- * @param sched scheduler to use
  * @param access function to use to check if access is allowed
  * @param access_cls closure for access
  * @param lsock listen socket
- * @param maxbuf maximum write buffer size for the socket (use
- *        0 for sockets that need no write buffers, such as listen sockets)
  * @return the socket handle, NULL on error
  */
 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)
+GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
+                                      void *access_cls,
+                                      struct GNUNET_NETWORK_Handle *lsock)
 {
   struct GNUNET_CONNECTION_Handle *ret;
-  char addr[32];
+  char addr[128];
   socklen_t addrlen;
   struct GNUNET_NETWORK_Handle *sock;
   int aret;
@@ -260,20 +362,23 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
   struct sockaddr_in6 *v6;
   struct sockaddr *sa;
   void *uaddr;
+  struct GNUNET_CONNECTION_Credentials *gcp;  
+  struct GNUNET_CONNECTION_Credentials gc;  
+#ifdef SO_PEERCRED
+  struct ucred uc;
+  socklen_t olen;
+#endif
 
   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))
+  if ( (addrlen > sizeof (addr)) ||
+       (addrlen < sizeof (sa_family_t)) )
     {
       GNUNET_break (0);
       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
@@ -288,6 +393,9 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
       v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
       memset (v4, 0, sizeof (struct sockaddr_in));
       v4->sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+      v4->sin_len = (u_char) sizeof (struct sockaddr_in);
+#endif
       memcpy (&v4->sin_addr,
               &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
                                          sizeof (struct in_addr)],
@@ -301,31 +409,72 @@ GNUNET_CONNECTION_create_from_accept (struct GNUNET_SCHEDULER_Handle
       uaddr = GNUNET_malloc (addrlen);
       memcpy (uaddr, addr, addrlen);
     }
+  gcp = NULL;
+  gc.uid = 0;
+  gc.gid = 0;
+  if (sa->sa_family == AF_UNIX)
+    {
+#if HAVE_GETPEEREID
+      /* most BSDs */
+      if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), 
+                          &gc.uid,
+                          &gc.gid))
+       gcp = &gc;
+#else
+#ifdef SO_PEERCRED
+      /* largely traditional GNU/Linux */
+      olen = sizeof (uc);
+      if ( (0 ==
+           getsockopt (GNUNET_NETWORK_get_fd (sock), 
+                       SOL_SOCKET, SO_PEERCRED, &uc, &olen)) &&
+          (olen == sizeof (uc)) )
+       {
+         gc.uid = uc.uid;
+         gc.gid = uc.gid;
+         gcp = &gc;    
+       }
+#else
+#if HAVE_GETPEERUCRED
+      /* this is for Solaris 10 */
+      ucred_t *uc;
+
+      uc = NULL;
+      if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
+       {
+         gc.uid = ucred_geteuid (uc);
+         gc.gid = ucred_getegid (uc);
+         gcp = &gc;
+       }
+      ucred_free (uc);
+#endif
+#endif
+#endif
+    }
 
   if ((access != NULL) &&
-      (GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
+      (GNUNET_YES != (aret = access (access_cls, gcp, 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;
+  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
+  ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
+  ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
   ret->addr = uaddr;
   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;
 }
 
@@ -339,7 +488,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;
@@ -351,230 +500,399 @@ GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
 
 
 /**
- * Perform a DNS lookup for the hostname associated
- * with the current socket, iterating over the address
- * families as specified in the "address_families" array.
+ * This function is called after establishing a connection either has
+ * succeeded or timed out.  Note that it is possible that the attempt
+ * timed out and that we're immediately retrying.  If we are retrying,
+ * we need to wait again (or timeout); if we succeeded, we need to
+ * wait for data (or timeout).
  *
- * @param sock the socket for which to do the lookup
+ * @param cls our connection handle
+ * @param tc task context describing why we are here
  */
 static void
-try_lookup (struct GNUNET_CONNECTION_Handle *sock)
+receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
+/**
+ * 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 addrinfo hints;
-  int ec;
+  struct GNUNET_CONNECTION_Handle *sock = cls;
+  GNUNET_CONNECTION_TransmitReadyNotify notify;
+  struct AddressProbe *pos;
 
-  GNUNET_assert (0 < strlen (sock->hostname)); /* sanity check */
-  while ( (sock->ai_pos == NULL) &&
-         (sock->af_fam_offset > 0) )
+  sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_assert (sock->dns_active == NULL);
+  if (0 != (sock->ccs & COCO_TRANSMIT_READY))
     {
-      if (sock->ai != NULL)
-       freeaddrinfo (sock->ai);
-      memset (&hints, 0, sizeof (hints));
-      hints.ai_family = address_families[--sock->af_fam_offset];
-      hints.ai_socktype = SOCK_STREAM;
-#if 0
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                 _("`%s' tries to resolve address family %d and hostname `%s:%u'\n"),
-                 "getaddrinfo", 
-                 address_families[sock->af_fam_offset],
-                 sock->hostname,
-                 sock->port);
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Destroy waits for CCS-TR to be done (%p)\n", sock);
 #endif
-      ec = getaddrinfo (sock->hostname, NULL, &hints, &sock->ai);
-#if 0
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                 _("`%s' returned from resolving address family %d and hostname `%s:%u'\n"),
-                 "getaddrinfo", 
-                 address_families[sock->af_fam_offset],
-                 sock->hostname,
-                 sock->port);
+      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
-      if (0 != ec)
-       {
-         GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
-                     _("`%s' failed for address family %d and hostname `%s:%u': %s\n"),
-                     "getaddrinfo", 
-                     address_families[sock->af_fam_offset],
-                     sock->hostname,
-                     sock->port,
-                     gai_strerror (ec));
-         sock->ai = NULL;
-       }
-      sock->ai_pos = sock->ai;
+      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
+      sock->destroy_task 
+       = GNUNET_SCHEDULER_add_after (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
+      if (sock->persist != GNUNET_YES)
+      {
+        if ( (GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) &&
+            (errno != ENOTCONN) &&
+             (errno != ECONNRESET) )
+          GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown");
+      }
+    }
+  if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
+      sock->destroy_task 
+       = GNUNET_SCHEDULER_add_after (sock->read_task,
+                                     &destroy_continuation, sock);
+      return;
+    }
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+             "Destroy actually runs (%p)!\n", sock);
+#endif
+  while (NULL != (pos = sock->ap_head))
+    {
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
+      GNUNET_SCHEDULER_cancel (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) 
+    {
+      if (sock->persist != GNUNET_YES)
+       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
+      else
+       GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
+                                    leak the socket in this special case) ... */
+    }
+  GNUNET_free_non_null (sock->addr);
+  GNUNET_free_non_null (sock->hostname);
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Freeing memory of connection %p.\n", sock);
+#endif
+  GNUNET_free (sock->write_buffer);
+  GNUNET_free (sock);
 }
 
 
+
 /**
- * Initiate asynchronous TCP connect request.
+ * See if we are now connected.  If not, wait longer for
+ * connect to succeed.  If connected, we should be able
+ * to write now as well, unless we timed out.
  *
- * @param sock what socket to connect
- * @return GNUNET_SYSERR error (no more addresses to try)
+ * @param cls our connection handle
+ * @param tc task context describing why we are here
  */
-static int
-try_connect (struct GNUNET_CONNECTION_Handle *sock)
-{
-  struct GNUNET_NETWORK_Handle *s;
+static void
+transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
 
-  if (sock->addr != NULL)
+/**
+ * We've failed for good to establish a connection.
+ *
+ * @param h the connection we tried to establish
+ */
+static void
+connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
+{
+#if DEBUG_CONNECTION
+  GNUNET_log ((0 != strncmp (h->hostname, 
+                            "localhost:",
+                            10)) 
+             ? GNUNET_ERROR_TYPE_INFO 
+             : GNUNET_ERROR_TYPE_WARNING,
+              _("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
+              h->hostname, h->port);
+#endif
+  /* connect failed / timed out */
+  GNUNET_break (h->ap_head == NULL);
+  GNUNET_break (h->ap_tail == NULL);
+  GNUNET_break (h->dns_active == GNUNET_NO);
+  GNUNET_break (h->sock == NULL);
+
+  /* trigger jobs that used to wait on "connect_task" */
+  if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
     {
-      GNUNET_free (sock->addr);
-      sock->addr = NULL;
-      sock->addrlen = 0;
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_fail_continuation triggers receive_again (%p)\n",
+                  h);
+#endif
+      h->ccs -= COCO_RECEIVE_AGAIN;
+      h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
     }
-  while (1)
+  if (0 != (h->ccs & COCO_TRANSMIT_READY))
     {
-      if (sock->ai_pos == NULL)
-       try_lookup (sock);
-      if (sock->ai_pos == NULL)
-        {
-          /* no more addresses to try, fatal! */
-          return GNUNET_SYSERR;
-        }
-      switch (sock->ai_pos->ai_family)
-        {
-        case AF_INET:
-          ((struct sockaddr_in *) sock->ai_pos->ai_addr)->sin_port =
-            htons (sock->port);
-          break;
-        case AF_INET6:
-          ((struct sockaddr_in6 *) sock->ai_pos->ai_addr)->sin6_port =
-            htons (sock->port);
-          break;
-        default:
-          sock->ai_pos = sock->ai_pos->ai_next;
-          continue;
-        }
-      s = GNUNET_NETWORK_socket_socket (sock->ai_pos->ai_family, SOCK_STREAM, 0);
-      if (s == NULL)
-        {
-          /* maybe unsupported address family, try next */
-          GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "socket");
-          sock->ai_pos = sock->ai_pos->ai_next;
-          continue;
-        }
-#ifndef MINGW
-      if (GNUNET_OK != GNUNET_NETWORK_socket_set_inheritable (s))
-        GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                             "GNUNET_NETWORK_socket_set_inheritable");
+#if DEBUG_CONNECTION
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
+                  h);
 #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 GNUNET_SYSERR;
-        }
+      GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
+      GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
+      h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
+      h->ccs -= COCO_TRANSMIT_READY;
+      GNUNET_assert (h->nth.notify_ready != NULL);
+      GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
+      h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
+    }
+  if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
+    {
 #if DEBUG_CONNECTION
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                 _("Trying to connect to `%s'\n"),
-                 GNUNET_a2s(sock->ai_pos->ai_addr,
-                            sock->ai_pos->ai_addrlen));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_fail_continuation runs destroy_continuation (%p)\n",
+                  h);
 #endif
-      if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s,
-                         sock->ai_pos->ai_addr,
-                         sock->ai_pos->ai_addrlen)) && (errno != EINPROGRESS))
-        {
-          /* maybe refused / unsupported address, try next */
-          GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
-          GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
-          sock->ai_pos = sock->ai_pos->ai_next;
-          continue;
-        }
-      break;
+      h->ccs -= COCO_DESTROY_CONTINUATION;
+      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
+      h->destroy_task
+       = GNUNET_SCHEDULER_add_now (&destroy_continuation,
+                                   h);
     }
-  /* got one! copy address information! */
-  sock->addrlen = sock->ai_pos->ai_addrlen;
-  sock->addr = GNUNET_malloc (sock->addrlen);
-  memcpy (sock->addr, sock->ai_pos->ai_addr, sock->addrlen);
-  sock->ai_pos = sock->ai_pos->ai_next;
-  sock->sock = s;
-  return GNUNET_OK;
 }
 
 
 /**
- * Scheduler let us know that we're either ready to
- * write on the socket OR connect timed out.  Do the
- * right thing.
+ * We've succeeded in establishing a connection.
+ *
+ * @param h the connection we tried to establish
  */
 static void
-connect_continuation (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext *tc)
+connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
 {
-  struct GNUNET_CONNECTION_Handle *sock = cls;
-  struct GNUNET_TIME_Relative delay;
-  unsigned int len;
-  int error;
-
-  GNUNET_assert (0 < strlen (sock->hostname)); /* sanity check */
-   /* nobody needs to wait for us anymore... */
-  sock->connect_task = GNUNET_SCHEDULER_NO_TASK;
-  /* Note: write-ready does NOT mean connect succeeded,
-     we need to use getsockopt to be sure */
-  len = sizeof (error);
-  errno = 0;
-  error = 0;
-  if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
-      (GNUNET_OK != GNUNET_NETWORK_socket_getsockopt (sock->sock, SOL_SOCKET, SO_ERROR, &error, &len)) ||
-      (error != 0) || (errno != 0))
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "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 & COCO_RECEIVE_AGAIN))
     {
 #if DEBUG_CONNECTION
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Failed to establish TCP connection to `%s:%u': %s\n",
-                 GNUNET_a2s(sock->addr, sock->addrlen),
-                 STRERROR (GNUNET_MAX (error, errno)));
+                  "connect_success_continuation runs receive_again (%p)\n",
+                  h);
 #endif
-      /* connect failed / timed out */
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
-      sock->sock = NULL;
-      if (GNUNET_SYSERR == try_connect (sock))
-        {
-          /* failed for good */
+      h->ccs -= COCO_RECEIVE_AGAIN;
+      h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
+    }
+  if (0 != (h->ccs & COCO_TRANSMIT_READY))
+    {
 #if DEBUG_CONNECTION
-         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                     "Failed to establish TCP connection to `%s:%u', no further addresses to try.\n",
-                     sock->hostname,
-                     sock->port);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
+                  h);
 #endif
-         /* connect failed / timed out */
-          GNUNET_break (sock->ai_pos == NULL);
-          freeaddrinfo (sock->ai);
-          sock->ai = NULL;
-          return;
-        }
-      delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
-      if (sock->nth.notify_ready != NULL)
-       delay = GNUNET_TIME_relative_min (delay,
-                                         GNUNET_TIME_absolute_get_remaining (sock->nth.transmit_timeout));
-      if (sock->receiver != NULL)
-       delay = GNUNET_TIME_relative_min (delay,
-                                         GNUNET_TIME_absolute_get_remaining (sock->receive_timeout));
-      delay.value /= (1 + sock->af_fam_offset);
+      GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
+      GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
+      h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
+      h->ccs -= COCO_TRANSMIT_READY;
+      GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
+      GNUNET_assert (h->nth.notify_ready != NULL);
+      h->write_task =
+        GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
+                                        (h->nth.transmit_timeout), h->sock,
+                                        &transmit_ready, h);
+    }
+  if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
+    {
 #if DEBUG_CONNECTION
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Will try to connect to `%s' for %llu ms\n",
-                 GNUNET_a2s (sock->addr,
-                             sock->addrlen),
-                 delay.value);
+                  "connect_success_continuation runs destroy_continuation (%p)\n",
+                  h);
 #endif
-      sock->connect_task = GNUNET_SCHEDULER_add_write_net (tc->sched, GNUNET_NO,    /* abort on shutdown */
-                                                          GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                          GNUNET_SCHEDULER_NO_TASK,
-                                                          delay,
-                                                          sock->sock,
-                                                          &connect_continuation,
-                                                          sock);
+      h->ccs -= COCO_DESTROY_CONTINUATION;
+      GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
+      h->destroy_task
+       = GNUNET_SCHEDULER_add_now (&destroy_continuation,
+                                               h);
+    }
+}
+
+
+/**
+ * Scheduler let us know that we're either ready to write on the
+ * socket OR connect timed out.  Do the right thing.
+ *
+ * @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)
+{
+  struct AddressProbe *ap = cls;
+  struct GNUNET_CONNECTION_Handle *h = ap->h;
+  struct AddressProbe *pos;
+  int error;
+  socklen_t len;
+
+  GNUNET_assert (ap->sock != NULL);
+  GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap);
+  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) )
+    {
+      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);
       return;
     }
-  /* connect succeeded! clean up "ai" */
+  GNUNET_assert (h->sock == NULL);
+  h->sock = ap->sock;
+  GNUNET_assert (h->addr == NULL);
+  h->addr = GNUNET_malloc (ap->addrlen);
+  memcpy (h->addr, ap->addr, ap->addrlen);
+  h->addrlen = ap->addrlen;
+  GNUNET_free (ap);
+  /* cancel all other attempts */
+  while (NULL != (pos = h->ap_head))
+    {
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
+      GNUNET_SCHEDULER_cancel (pos->task);
+      GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
+      GNUNET_free (pos);
+    }
+  connect_success_continuation (h);
+}
+
+
+/**
+ * Try to establish a socket connection given the specified address.
+ * This function is called by the resolver once we have a DNS reply.
+ *
+ * @param cls our "struct GNUNET_CONNECTION_Handle *"
+ * @param addr address to try, NULL for "last call"
+ * @param addrlen length of addr
+ */
+static void
+try_connect_using_address (void *cls,
+                           const struct sockaddr *addr, socklen_t addrlen)
+{
+  struct GNUNET_CONNECTION_Handle *h = cls;
+  struct AddressProbe *ap;
+  struct GNUNET_TIME_Relative delay;
+
+  if (addr == NULL)
+    {
+      h->dns_active = NULL;
+      if (NULL == h->ap_head)
+        connect_fail_continuation (h);
+      return;
+    }
+  if (h->sock != NULL)
+    return;                     /* already connected */
+  GNUNET_assert (h->addr == NULL);
+  /* try to connect */
 #if DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Connection to `%s' succeeded!\n",
-             GNUNET_a2s(sock->addr, sock->addrlen));
+              "Trying to connect using address `%s:%u/%s:%u'\n",
+              h->hostname, h->port, GNUNET_a2s (addr, addrlen), h->port);
 #endif
-  freeaddrinfo (sock->ai);
-  sock->ai_pos = NULL;
-  sock->ai = NULL;
+  ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
+  ap->addr = (const struct sockaddr *) &ap[1];
+  memcpy (&ap[1], addr, addrlen);
+  ap->addrlen = addrlen;
+  ap->h = h;
+
+  switch (ap->addr->sa_family)
+    {
+    case AF_INET:
+      ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
+      break;
+    case AF_INET6:
+      ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
+      break;
+    default:
+      GNUNET_break (0);
+      GNUNET_free (ap);
+      return;                   /* not supported by us */
+    }
+  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 */
+    }
+#if DEBUG_CONNECTION
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              _("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))
+    {
+      /* maybe refused / unsupported address, try next */
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
+      GNUNET_free (ap);
+      return;
+    }
+  GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
+  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));
+  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 (delay, ap->sock,
+                                    &connect_probe_continuation, ap);
 }
 
 
@@ -583,45 +901,99 @@ connect_continuation (void *cls,
  * This function returns immediately, even if the connection has not
  * yet been established.  This function only creates TCP connections.
  *
- * @param sched scheduler to use
+ * @param cfg configuration to use
  * @param hostname name of the host to connect to
  * @param port port to connect to
- * @param maxbuf maximum write buffer size for the socket (use
- *        0 for sockets that need no write buffers, such as listen sockets)
  * @return the socket handle
  */
 struct GNUNET_CONNECTION_Handle *
-GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle
-                                           *sched, const char *hostname,
-                                           uint16_t port, size_t maxbuf)
+GNUNET_CONNECTION_create_from_connect (const struct
+                                       GNUNET_CONFIGURATION_Handle *cfg,
+                                       const char *hostname, uint16_t port)
 {
   struct GNUNET_CONNECTION_Handle *ret;
 
-  GNUNET_assert (0 < strlen (hostname)); /* sanity check */
-  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle) + maxbuf);
-  ret->sock = NULL;
-  ret->sched = sched;
-  ret->write_buffer = (char *) &ret[1];
-  ret->write_buffer_size = maxbuf;
+  GNUNET_assert (0 < strlen (hostname));        /* sanity check */
+  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
+  ret->cfg = cfg;
+  ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
+  ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
   ret->port = port;
-  ret->af_fam_offset = sizeof (address_families) / sizeof(address_families[0]);
   ret->hostname = GNUNET_strdup (hostname);
-  if (GNUNET_SYSERR == try_connect (ret))
+  ret->dns_active = GNUNET_RESOLVER_ip_get (ret->hostname,
+                                            AF_UNSPEC,
+                                            GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
+                                            &try_connect_using_address, ret);
+  return ret;
+}
+
+
+/**
+ * Create a socket handle by connecting to a UNIX domain service.
+ * This function returns immediately, even if the connection has not
+ * yet been established.  This function only creates UNIX connections.
+ *
+ * @param cfg configuration to use
+ * @param unixpath path to connect to
+ * @return the socket handle, NULL on systems without UNIX support
+ */
+struct GNUNET_CONNECTION_Handle *
+GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
+                                                  GNUNET_CONFIGURATION_Handle *cfg,
+                                                  const char *unixpath)
+{
+#ifdef AF_UNIX
+  struct GNUNET_CONNECTION_Handle *ret;
+  struct sockaddr_un *un;
+  size_t slen;
+
+  GNUNET_assert (0 < strlen (unixpath));        /* sanity check */
+  un = GNUNET_malloc (sizeof (struct sockaddr_un));
+  un->sun_family = AF_UNIX;
+  slen = strlen (unixpath);
+  if (slen >= sizeof (un->sun_path))
+    slen = sizeof (un->sun_path) - 1;
+  memcpy (un->sun_path,
+         unixpath,
+         slen);
+  un->sun_path[slen] = '\0';
+  slen = sizeof (struct sockaddr_un);
+#if HAVE_SOCKADDR_IN_SIN_LEN
+  un->sun_len = (u_char) slen;
+#endif
+#if LINUX
+  un->sun_path[0] = '\0';
+#endif
+  ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
+  ret->cfg = cfg;
+  ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
+  ret->write_buffer = GNUNET_malloc(ret->write_buffer_size);
+  ret->port = 0;
+  ret->hostname = NULL;
+  ret->addr = (struct sockaddr*) un;
+  ret->addrlen = slen;
+  ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
+  if (NULL == ret->sock)
     {
-      if (NULL != ret->ai)
-       freeaddrinfo (ret->ai);
-      GNUNET_free (ret->hostname);
+      GNUNET_free (ret->addr);
+      GNUNET_free (ret->write_buffer);
       GNUNET_free (ret);
       return NULL;
     }
-  ret->connect_task = GNUNET_SCHEDULER_add_write_net (sched, GNUNET_NO,     /* abort on shutdown */
-                                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                  GNUNET_SCHEDULER_NO_TASK,
-                                                  GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
-                                                  ret->sock,
-                                                  &connect_continuation, ret);
+  if (GNUNET_OK != GNUNET_NETWORK_socket_connect (ret->sock,
+                                                 ret->addr,
+                                                 ret->addrlen)) 
+    {
+      /* Just return; we expect everything to work eventually so don't fail HARD */
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
+      ret->sock = NULL;
+      return ret;
+    }
+  connect_success_continuation (ret);
   return ret;
-
+#else
+  return NULL;
+#endif
 }
 
 
@@ -630,60 +1002,47 @@ GNUNET_CONNECTION_create_from_connect (struct GNUNET_SCHEDULER_Handle
  * This function returns immediately, even if the connection has not
  * yet been established.  This function only creates TCP connections.
  *
- * @param sched scheduler to use
  * @param af_family address family to use
  * @param serv_addr server address
  * @param addrlen length of server address
- * @param maxbuf maximum write buffer size for the socket (use
- *        0 for sockets that need no write buffers, such as listen sockets)
  * @return the socket handle
  */
 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)
+GNUNET_CONNECTION_create_from_sockaddr (int af_family,
+                                        const struct sockaddr *serv_addr,
+                                        socklen_t addrlen)
 {
   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");
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+                 _("Attempt to connect to `%s' failed\n"),
+                 GNUNET_a2s (serv_addr, addrlen));
       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
       return NULL;
     }
-  ret = GNUNET_CONNECTION_create_from_existing (sched, s, maxbuf);
+  ret = GNUNET_CONNECTION_create_from_existing (s);
   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;
 }
 
@@ -699,89 +1058,51 @@ GNUNET_CONNECTION_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle
 int
 GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
 {
-  if (sock->ai != NULL)
+  if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
     return GNUNET_YES;          /* still trying to connect */
   return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
 }
 
 
 /**
- * Scheduler let us know that the connect task is finished (or was
- * cancelled due to shutdown).  Now really clean up.
+ * Close the socket and free associated resources. Pending
+ * 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').
  */
-static void
-destroy_continuation (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext *tc)
+void
+GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
+                          int finish_pending_write)
 {
-  struct GNUNET_CONNECTION_Handle *sock = cls;
-  GNUNET_CONNECTION_TransmitReadyNotify notify;
-
-  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 (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
+  if (GNUNET_NO == finish_pending_write)
     {
-      GNUNET_SCHEDULER_add_after (sock->sched,
-                                  GNUNET_YES,
-                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                  sock->read_task,
-                                  &destroy_continuation, sock);
-      return;
+      if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
+       {
+         GNUNET_SCHEDULER_cancel (sock->write_task);
+         sock->write_task = GNUNET_SCHEDULER_NO_TASK;
+         sock->write_buffer_off = 0;
+       }
+      sock->nth.notify_ready = NULL;
     }
-  if (NULL != (notify = sock->nth.notify_ready))
+  if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
     {
-      sock->nth.notify_ready = NULL;
-      notify (sock->nth.notify_ready_cls, 0, NULL);
-      if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK)
-        {
-          GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
-          sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
-        }
+      GNUNET_RESOLVER_request_cancel (sock->dns_active);
+      sock->dns_active = NULL;
     }
-  if (sock->sock != NULL)
-    GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
-  GNUNET_free_non_null (sock->addr);
-  if (sock->ai != NULL)
-    freeaddrinfo (sock->ai);      
-  GNUNET_free_non_null (sock->hostname);
-  GNUNET_free (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".
- *
- * @param sock socket to destroy
- */
-void
-GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock)
-{
-  if (sock->write_buffer_off == 0)
-    sock->ai_pos = NULL;        /* if we're still trying to connect and have
-                                   no message pending, stop trying! */
-  GNUNET_assert (sock->sched != NULL);
-  GNUNET_SCHEDULER_add_after (sock->sched,
-                              GNUNET_YES,
-                              GNUNET_SCHEDULER_PRIORITY_KEEP,
-                              sock->connect_task,
-                              &destroy_continuation, sock);
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
+  sock->destroy_task 
+    = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
 }
 
+
 /**
  * Tell the receiver callback that a timeout was reached.
  */
@@ -792,7 +1113,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;
@@ -827,16 +1148,32 @@ 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 (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) ||
+  if ((now.abs_value > sh->receive_timeout.abs_value) ||
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
     {
 #if DEBUG_CONNECTION
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Receive from %s encounters error: time out by %llums...\n",
-                 GNUNET_a2s (sh->addr, sh->addrlen),
-                 now.value - sh->receive_timeout.value);
+      if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+        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).
+                    rel_value, sh);
 #endif
       signal_timeout (sh);
       return;
@@ -846,21 +1183,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)
@@ -874,10 +1204,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;
@@ -891,6 +1220,9 @@ RETRY:
  * timed out and that we're immediately retrying.  If we are retrying,
  * we need to wait again (or timeout); if we succeeded, we need to
  * wait for data (or timeout).
+ *
+ * @param cls our connection handle
+ * @param tc task context describing why we are here
  */
 static void
 receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -899,47 +1231,33 @@ 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) &&
-      (sh->connect_task == GNUNET_SCHEDULER_NO_TASK))
+  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;
     }
   now = GNUNET_TIME_absolute_get ();
-  if ((now.value > sh->receive_timeout.value) ||
+  if ((now.abs_value > sh->receive_timeout.abs_value) ||
       (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
     {
 #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;
     }
-  if (sh->connect_task != GNUNET_SCHEDULER_NO_TASK)
-    {
-      /* connect was retried */
-      sh->read_task = GNUNET_SCHEDULER_add_after (tc->sched,
-                                                  GNUNET_YES,
-                                                  GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                  sh->connect_task,
-                                                  &receive_again, sh);
-      return;
-    }
+  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,
-                                            sh->connect_task,
-                                            GNUNET_TIME_absolute_get_remaining
-                                            (sh->receive_timeout),
-                                            sh->sock, &receive_ready,
-                                            sh);
+  sh->read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
+                                                 (sh->receive_timeout),
+                                                 sh->sock,
+                                                 &receive_ready, sh);
 }
 
 
@@ -950,33 +1268,55 @@ 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")
  * @param receiver function to call with received data
  * @param receiver_cls closure for receiver
- * @return scheduler task ID used for receiving, GNUNET_SCHEDULER_NO_TASK on error
  */
-GNUNET_SCHEDULER_TaskIdentifier
+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 & COCO_RECEIVE_AGAIN)) &&
                  (sock->receiver == NULL));
   sock->receiver = receiver;
   sock->receiver_cls = receiver_cls;
   sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
   sock->max = max;
-  memset (&tc, 0, sizeof (tc));
-  tc.sched = sock->sched;
-  tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
-  receive_again (sock, &tc);
-  return sock->read_task;
+  if (sock->sock != NULL)
+    {
+      memset (&tc, 0, sizeof (tc));
+      tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
+      receive_again (sock, &tc);
+      return;
+    }
+  if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
+    {
+      receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
+      return;
+    }
+  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;
 }
 
 
@@ -986,16 +1326,21 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock,
  * for the cancellation to be valid.
  *
  * @param sock socket handle
- * @param task task identifier returned from the receive call
- * @return closure of the original receiver callback
+ * @return closure of the original receiver callback closure
  */
 void *
-GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock,
-                               GNUNET_SCHEDULER_TaskIdentifier task)
+GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
 {
-  GNUNET_assert (sock->read_task == task);
-  GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->sched, task));
-  sock->read_task = GNUNET_SCHEDULER_NO_TASK;
+  if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
+      sock->read_task = GNUNET_SCHEDULER_NO_TASK;
+    }
+  else
+    {
+      GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
+      sock->ccs -= COCO_RECEIVE_AGAIN;
+    }
   sock->receiver = NULL;
   return sock->receiver_cls;
 }
@@ -1022,14 +1367,9 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
   used = sock->write_buffer_off - sock->write_buffer_pos;
   avail = sock->write_buffer_size - used;
   size = sock->nth.notify_size;
-  if (sock->nth.notify_size > avail)
+  if (size > avail)
     return GNUNET_NO;
   sock->nth.notify_ready = NULL;
-  if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK)
-    {
-      GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
-      sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
-    }
   if (sock->write_buffer_size - sock->write_buffer_off < size)
     {
       /* need to compact */
@@ -1038,10 +1378,12 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
       sock->write_buffer_off -= sock->write_buffer_pos;
       sock->write_buffer_pos = 0;
     }
-  GNUNET_assert (sock->write_buffer_size - sock->write_buffer_off >= size);
+  avail = sock->write_buffer_size - sock->write_buffer_off;
+  GNUNET_assert (avail >= size);
   size = notify (sock->nth.notify_ready_cls,
-                 sock->write_buffer_size - sock->write_buffer_off,
+                 avail,
                  &sock->write_buffer[sock->write_buffer_off]);
+  GNUNET_assert (size <= avail);
   sock->write_buffer_off += size;
   return GNUNET_YES;
 }
@@ -1054,35 +1396,96 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
  * expired).
  *
  * This task notifies the client about the timeout.
+ *
+ * @param cls the 'struct GNUNET_CONNECTION_Handle'
+ * @param tc scheduler context
  */
 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 to `%s' fails, time out reached.\n",
-             GNUNET_a2s (sock->addr, sock->addrlen));
+  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 (%p).\n",
+              sock->hostname,
+              sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock);
+#endif
+  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.
+ *
+ * @param cls the 'struct GNUNET_CONNECTION_Handle'
+ * @param tc scheduler context
+ */
+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 (%s/%u), connection failed (%p).\n",
+              sock->nth.notify_size, 
+             sock->hostname,
+             sock->port,
+             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);
 }
 
 
+/**
+ * FIXME
+ *
+ * @param sock FIXME
+ */
 static void
 transmit_error (struct GNUNET_CONNECTION_Handle *sock)
 {
-  if (sock->nth.notify_ready == NULL)
-    return;                     /* nobody to tell about it */
-  if (sock->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK)
+  GNUNET_CONNECTION_TransmitReadyNotify notify;
+
+  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;
+    }
+  if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
     {
-      GNUNET_SCHEDULER_cancel (sock->sched, sock->nth.timeout_task);
-      sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
+      GNUNET_SCHEDULER_cancel (sock->read_task);
+      sock->read_task = GNUNET_SCHEDULER_NO_TASK;
+      signal_timeout (sock);
+      return;
     }
-  transmit_timeout (sock, NULL);
+  if (sock->nth.notify_ready == NULL)
+    return;                     /* nobody to tell about it */
+  notify = sock->nth.notify_ready;
+  sock->nth.notify_ready = NULL;
+  notify (sock->nth.notify_ready_cls, 0, NULL);
 }
 
 
@@ -1090,55 +1493,82 @@ transmit_error (struct GNUNET_CONNECTION_Handle *sock)
  * See if we are now connected.  If not, wait longer for
  * connect to succeed.  If connected, we should be able
  * to write now as well, unless we timed out.
+ *
+ * @param cls our connection handle
+ * @param tc task context describing why we are here
  */
 static void
 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CONNECTION_Handle *sock = cls;
+  GNUNET_CONNECTION_TransmitReadyNotify notify;
   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;
-  if (sock->connect_task != GNUNET_SCHEDULER_NO_TASK)
+  GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 
     {
-      /* still waiting for connect */
-      GNUNET_assert (sock->write_task ==
-                     GNUNET_SCHEDULER_NO_TASK);
-      sock->write_task =
-        GNUNET_SCHEDULER_add_delayed (tc->sched, GNUNET_NO,
-                                      GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                      sock->connect_task,
-                                      GNUNET_TIME_UNIT_ZERO, &transmit_ready,
-                                      sock);
+      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;
+      if (NULL != notify)
+       {
+         sock->nth.notify_ready = NULL;
+         notify (sock->nth.notify_ready_cls, 0, NULL);
+       }
       return;
     }
-  if ( (sock->sock == NULL) ||
-       ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) &&
-        (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) &&
-        (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock)))  )
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
     {
 #if DEBUG_CONNECTION
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  _("Could not satisfy pending transmission request, socket closed or connect failed.\n"));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Transmit to `%s' fails, time out reached (%p).\n",
+                  GNUNET_a2s (sock->addr, sock->addrlen), 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;
-       }
-      transmit_error (sock);
-      return;                   /* connect failed for good, we're finished */
+      notify = sock->nth.notify_ready;
+      GNUNET_assert (NULL != notify);
+      sock->nth.notify_ready = NULL;
+      notify (sock->nth.notify_ready_cls, 0, NULL);
+      return;
     }
- if ((tc->write_ready == NULL) || (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock)))
+  GNUNET_assert (NULL != 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.  */
+         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 (%p).\n"),
+                  sock);
+#endif
+      transmit_error (sock);
+      return;                   /* connect failed for good, we're finished */
+    }
   GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
+  if ( (sock->nth.notify_ready != NULL) &&
+       (sock->write_buffer_size < sock->nth.notify_size) )
+    {
+      sock->write_buffer = GNUNET_realloc(sock->write_buffer, 
+                                         sock->nth.notify_size);
+      sock->write_buffer_size = sock->nth.notify_size;
+    }    
   process_notify (sock);
   have = sock->write_buffer_off - sock->write_buffer_pos;
   if (have == 0)
@@ -1146,36 +1576,37 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       /* 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)
         goto RETRY;
+#if 0
+      int en = errno;
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 _("Failed to send to `%s': %s\n"),
+                 GNUNET_a2s (sock->addr,
+                             sock->addrlen),
+                 STRERROR (en));
+#endif
 #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;
       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)
@@ -1188,14 +1619,19 @@ RETRY:
     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
+  have = sock->write_buffer_off - sock->write_buffer_pos;
+  GNUNET_assert ( (sock->nth.notify_ready != NULL) || (have > 0) );
   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_SCHEDULER_add_write_net ((sock->nth.notify_ready == NULL) 
+                                     ? GNUNET_TIME_UNIT_FOREVER_REL 
+                                     : GNUNET_TIME_absolute_get_remaining (sock->nth.transmit_timeout),
+                                     sock->sock, 
+                                     &transmit_ready, sock);
 }
 
 
@@ -1215,27 +1651,18 @@ 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->connect_task == GNUNET_SCHEDULER_NO_TASK))
     {
-#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 (0);
+      return NULL;
     }
+  GNUNET_assert (notify != NULL);
+  GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
   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);
@@ -1244,55 +1671,77 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
   sock->nth.sh = sock;
   sock->nth.notify_size = size;
   sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  sock->nth.timeout_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
-                                                         GNUNET_NO,
-                                                         GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                         GNUNET_SCHEDULER_NO_TASK,
-                                                         timeout,
-                                                         &transmit_timeout,
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
+  if ((sock->sock == NULL) &&
+      (sock->ap_head == NULL) && (sock->dns_active == NULL))
+    {
+      if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
+       GNUNET_SCHEDULER_cancel (sock->write_task);
+      sock->write_task = GNUNET_SCHEDULER_add_now (&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 (GNUNET_TIME_absolute_get_remaining
+                                                         (sock->nth.
+                                                          transmit_timeout),
+                                                         sock->sock,
+                                                         &transmit_ready,
                                                          sock);
-  if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
+    }
+  else
     {
-      if (sock->connect_task == GNUNET_SCHEDULER_NO_TASK)
-       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);
-      else
-       sock->write_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
-                                                        GNUNET_NO,
-                                                        GNUNET_SCHEDULER_PRIORITY_KEEP,
-                                                        sock->connect_task,
-                                                        GNUNET_TIME_UNIT_ZERO,
-                                                        &transmit_ready, sock);
+#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 (timeout,
+                                                             &transmit_timeout,
+                                                             sock);
     }
   return &sock->nth;
 }
 
 
 /**
- * Cancel the specified transmission-ready
- * notification.
+ * Cancel the specified transmission-ready notification.
+ *
+ * @param th notification to cancel
  */
 void
 GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
-                                             GNUNET_CONNECTION_TransmitHandle *h)
+                                                GNUNET_CONNECTION_TransmitHandle
+                                                *h)
 {
   GNUNET_assert (h->notify_ready != NULL);
-  GNUNET_SCHEDULER_cancel (h->sh->sched, h->timeout_task);
-  h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-  h->notify_ready = NULL;
-}
-
-
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
+  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
-#ifdef __cplusplus
+      GNUNET_SCHEDULER_cancel (h->timeout_task);
+      h->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+      h->sh->ccs -= COCO_TRANSMIT_READY;
+    }
+  else
+    {
+      if (h->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
+       {
+         GNUNET_SCHEDULER_cancel (h->sh->write_task);
+         h->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
+       }
+    }
+  h->notify_ready = NULL;
 }
-#endif
 
 /* end of connection.c */