Index: lwip/src/lwip/include/lwip/api.h
===================================================================
--- lwip/src/lwip/include/lwip/api.h	(revision 1)
+++ lwip/src/lwip/include/lwip/api.h	(revision 2)
@@ -83,7 +83,8 @@
   NETCONN_EVT_RCVPLUS,
   NETCONN_EVT_RCVMINUS,
   NETCONN_EVT_SENDPLUS,
-  NETCONN_EVT_SENDMINUS
+  NETCONN_EVT_SENDMINUS,
+  NETCONN_EVT_ERROR
 };
 
 #if LWIP_IGMP
@@ -152,6 +153,11 @@
 #endif /* LWIP_TCPIP_CORE_LOCKING */
   /** A callback function that is informed about events for this netconn */
   netconn_callback callback;
+
+  /** Should this netconnection avoid blocking? */
+  u8_t non_blocking;
+  /** Was the last connect action non-blocking one? */
+  u8_t in_non_blocking_connect;
 };
 
 /* Register an Network connection event */
Index: lwip/src/lwip/api/api_msg.c
===================================================================
--- lwip/src/lwip/api/api_msg.c	(revision 1)
+++ lwip/src/lwip/api/api_msg.c	(revision 2)
@@ -292,14 +292,24 @@
   conn->pcb.tcp = NULL;
 
   conn->err = err;
+  // Notify the upper layer about a connection error, without even checking the 
+  // connection state.
+  API_EVENT(conn, NETCONN_EVT_ERROR, 0);
   if (conn->recvmbox != SYS_MBOX_NULL) {
     /* Register event with callback */
     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
     sys_mbox_post(conn->recvmbox, NULL);
   }
+
   if (conn->op_completed != SYS_SEM_NULL && conn->state == NETCONN_CONNECT) {
     conn->state = NETCONN_NONE;
-    sys_sem_signal(conn->op_completed);
+	// Signal both send and error - as required by POSIX. 
+	// Signal error first, to help applications get the more informative 
+	// notification first.
+	API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
+	if (!conn->in_non_blocking_connect) {
+		sys_sem_signal(conn->op_completed);
+	}
   }
   if (conn->acceptmbox != SYS_MBOX_NULL) {
     /* Register event with callback */
@@ -310,6 +320,8 @@
     /* calling do_writemore/do_close_internal is not necessary
        since the pcb has already been deleted! */
     conn->state = NETCONN_NONE;
+    /* Release "write" selects, to ensure that the system wouldn't get stuck. */
+	API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
     /* wake up the waiting task */
     sys_sem_signal(conn->op_completed);
   }
@@ -536,6 +548,8 @@
 #if LWIP_SO_RCVBUF
   conn->recv_bufsize = INT_MAX;
 #endif /* LWIP_SO_RCVBUF */
+  conn->non_blocking = 0;
+  conn->in_non_blocking_connect = 0;
   return conn;
 }
 
@@ -619,8 +633,9 @@
     /* Set back some callback pointers as conn is going away */
     conn->pcb.tcp = NULL;
     conn->err = ERR_OK;
-    /* Trigger select() in socket layer. This send should something else so the
-       errorfd is set, not the read and write fd! */
+    /* Trigger select() in socket layer. Make sure everybody notices activity
+	   on the connection. */
+	API_EVENT(conn, NETCONN_EVT_ERROR, 0);
     API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
     API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
     /* wake up the application task */
@@ -750,7 +765,13 @@
     setup_tcp(conn);
   }
   conn->state = NETCONN_NONE;
-  sys_sem_signal(conn->op_completed);
+  if (!conn->in_non_blocking_connect) {
+	  API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
+	  sys_sem_signal(conn->op_completed);
+  } else {
+	  conn->in_non_blocking_connect = 0;
+	  API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
+  }
   return ERR_OK;
 }
 #endif /* LWIP_TCP */
@@ -769,6 +790,13 @@
     sys_sem_signal(msg->conn->op_completed);
     return;
   }
+  
+  // Prevent connect while doing a connect.
+  if (msg->conn->in_non_blocking_connect ) {
+	msg->conn->err = ERR_ISCONN;
+    sys_sem_signal(msg->conn->op_completed);
+    return;
+  }
 
   switch (NETCONNTYPE_GROUP(msg->conn->type)) {
 #if LWIP_RAW
@@ -787,13 +815,21 @@
   case NETCONN_TCP:
     msg->conn->state = NETCONN_CONNECT;
     setup_tcp(msg->conn);
+	msg->conn->in_non_blocking_connect = msg->conn->non_blocking;
     msg->conn->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
                                  do_connected);
+	if (msg->conn->non_blocking ) {
+		if (msg->conn->err == ERR_OK) {
+			msg->conn->err = ERR_INPROGRESS;
+		}
+		sys_sem_signal(msg->conn->op_completed);
+	}
     /* sys_sem_signal() is called from do_connected (or err_tcp()),
      * when the connection is established! */
     break;
 #endif /* LWIP_TCP */
   default:
+	  // BAD!!!!!!! Would block indefenitely. We should return an error value.
     break;
   }
 }
@@ -1038,7 +1074,9 @@
 do_write(struct api_msg_msg *msg)
 {
   if (!ERR_IS_FATAL(msg->conn->err)) {
-    if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
+	if(msg->conn->state == NETCONN_CONNECT) {
+	  msg->conn->err = ERR_INPROGRESS;
+	} else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
 #if LWIP_TCP
       msg->conn->state = NETCONN_WRITE;
       /* set all the variables used by do_writemore */
Index: lwip/src/lwip/api/sockets.c
===================================================================
--- lwip/src/lwip/api/sockets.c (revision 1)
+++ lwip/src/lwip/api/sockets.c	(revision 2)
@@ -70,6 +70,9 @@
   /** number of times data was received, set by event_callback(),
       tested by select */
   u16_t sendevent;
+  /** error happend for this socket, set by event_callback(),
+      tested by select */
+  u16_t errevent;
   /** socket flags (currently, only used for O_NONBLOCK) */
   u16_t flags;
   /** last error that occurred on this socket */
@@ -152,7 +155,7 @@
 #ifdef ERRNO
 #define set_errno(err) errno = (err)
 #else
-#define set_errno(err)
+#define set_errno(err) _set_errno(err)
 #endif
 
 #define sock_set_errno(sk, e) do { \
@@ -228,6 +231,7 @@
       sockets[i].sendevent  = 1; /* TCP send buf is empty */
       sockets[i].flags      = 0;
       sockets[i].err        = 0;
+	  sockets[i].errevent   = 0;
       sys_sem_signal(socksem);
       return i;
     }
@@ -403,12 +407,18 @@
     ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
     LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%u)\n", ntohs(remote_port)));
 
+	sock->sendevent = 0;
+	sock->conn->non_blocking = ((sock->flags & O_NONBLOCK) == O_NONBLOCK);
     err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
   }
 
   if (err != ERR_OK) {
     LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
     sock_set_errno(sock, err_to_errno(err));
+	// Allow select to work if we are non-blocking.
+	if (ERR_INPROGRESS == err) {
+		sock->err = 0;
+	}
     return -1;
   }
 
@@ -830,10 +840,20 @@
         nready++;
       }
     }
+    if (FD_ISSET(i, exceptset)) {
+      /* See if netconn of this socket is ready for write */
+      p_sock = get_socket(i);
+      if (p_sock && p_sock->errevent) {
+        FD_SET(i, &lexceptset);
+        LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for exception\n", i));
+        nready++;
+      }
+    }
+
   }
   *readset = lreadset;
   *writeset = lwriteset;
-  FD_ZERO(exceptset);
+  *exceptset = lexceptset;
   
   return nready;
 }
@@ -1043,6 +1063,13 @@
     case NETCONN_EVT_SENDMINUS:
       sock->sendevent = 0;
       break;
+	case NETCONN_EVT_ERROR:
+	  // TODO: Would only set the errno in tcpip_thread. Is this what we want?
+	  // Disable for now, as we want only to notify the client that the socket
+	  // is in error, and setting the errno in the tcpip_thread wouldn't help.
+	  //sock_set_errno(sock,err_to_errno(conn->err));
+	  sock->errevent = 1;
+	  break;
     default:
       LWIP_ASSERT("unknown event", 0);
       break;
@@ -1067,6 +1094,9 @@
         if (scb->writeset && FD_ISSET(s, scb->writeset))
           if (sock->sendevent)
             break;
+		if (scb->exceptset && FD_ISSET(s, scb->exceptset))
+			if (sock->errevent != 0)
+				break;
       }
     }
     if (scb) {

