Index: src/api/api_lib.c
===================================================================
RCS file: /sources/lwip/lwip/src/api/api_lib.c,v
retrieving revision 1.83
diff -w -b -u -2 -p -r1.83 api_lib.c
--- src/api/api_lib.c	16 Dec 2007 16:15:08 -0000	1.83
+++ src/api/api_lib.c	19 Dec 2007 21:22:57 -0000
@@ -303,4 +303,14 @@ netconn_accept(struct netconn *conn)
   API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
 
+#if LWIP_LISTEN_BACKLOG
+  if (newconn != NULL) {
+    /* Let the stack know that we have accepted the connection. */
+    struct api_msg msg;
+    msg.function = do_recv;
+    msg.msg.conn = conn;
+    TCPIP_APIMSG(&msg);
+  }
+#endif /* LWIP_LISTEN_BACKLOG */
+
   return newconn;
 }
Index: src/api/api_msg.c
===================================================================
RCS file: /sources/lwip/lwip/src/api/api_msg.c,v
retrieving revision 1.87
diff -w -b -u -2 -p -r1.87 api_msg.c
--- src/api/api_msg.c	13 Dec 2007 23:06:50 -0000	1.87
+++ src/api/api_msg.c	19 Dec 2007 21:48:14 -0000
@@ -728,4 +728,7 @@ do_listen(struct api_msg_msg *msg)
               msg->conn->state = NETCONN_LISTEN;
               msg->conn->pcb.tcp = lpcb;
+#if LWIP_LISTEN_BACKLOG
+              tcp_backlog(lpcb, msg->msg.lb.backlog);
+#endif /* LWIP_LISTEN_BACKLOG */
               tcp_arg(msg->conn->pcb.tcp, msg->conn);
               tcp_accept(msg->conn->pcb.tcp, accept_function);
@@ -793,8 +796,14 @@ do_recv(struct api_msg_msg *msg)
     if (msg->conn->pcb.tcp != NULL) {
       if (msg->conn->type == NETCONN_TCP) {
+        if (msg->conn->pcb.tcp->state == LISTEN) {
+#if LWIP_LISTEN_BACKLOG
+          tcp_accepted(msg->conn->pcb.tcp);
+#endif /* LWIP_LISTEN_BACKLOG */
+        } else {
         tcp_recved(msg->conn->pcb.tcp, msg->msg.r.len);
       }
     }
   }
+  }
 #endif /* LWIP_TCP */
   TCPIP_APIMSG_ACK(msg);
Index: src/core/tcp.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/tcp.c,v
retrieving revision 1.76
diff -w -b -u -2 -p -r1.76 tcp.c
--- src/core/tcp.c	4 Dec 2007 20:43:10 -0000	1.76
+++ src/core/tcp.c	19 Dec 2007 21:19:37 -0000
@@ -369,4 +369,8 @@ tcp_listen(struct tcp_pcb *pcb)
   lpcb->accept = tcp_accept_null;
 #endif /* LWIP_CALLBACK_API */
+#if LWIP_LISTEN_BACKLOG
+  lpcb->to_accept = 0;
+  lpcb->backlog = 0xff;
+#endif /* LWIP_LISTEN_BACKLOG */
   TCP_REG(&tcp_listen_pcbs.listen_pcbs, lpcb);
   return (struct tcp_pcb *)lpcb;
Index: src/core/tcp_in.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/tcp_in.c,v
retrieving revision 1.90
diff -w -b -u -2 -p -r1.90 tcp_in.c
--- src/core/tcp_in.c	16 Dec 2007 16:24:10 -0000	1.90
+++ src/core/tcp_in.c	19 Dec 2007 21:08:03 -0000
@@ -382,4 +382,9 @@ tcp_listen_input(struct tcp_pcb_listen *
   } else if (flags & TCP_SYN) {
     LWIP_DEBUGF(TCP_DEBUG, ("TCP connection request %"U16_F" -> %"U16_F".\n", tcphdr->src, tcphdr->dest));
+#if LWIP_LISTEN_BACKLOG
+    if (pcb->to_accept >= pcb->backlog) {
+      return ERR_ABRT;
+    }
+#endif /* LWIP_LISTEN_BACKLOG */
     npcb = tcp_alloc(pcb->prio);
     /* If a new PCB could not be created (probably due to lack of memory),
@@ -391,4 +396,7 @@ tcp_listen_input(struct tcp_pcb_listen *
       return ERR_MEM;
     }
+#if LWIP_LISTEN_BACKLOG
+    pcb->to_accept++;
+#endif /* LWIP_LISTEN_BACKLOG */
     /* Set up the new PCB. */
     ip_addr_set(&(npcb->local_ip), &(iphdr->dest));
Index: src/include/lwip/api_msg.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/api_msg.h,v
retrieving revision 1.27
diff -w -b -u -2 -p -r1.27 api_msg.h
--- src/include/lwip/api_msg.h	2 Dec 2007 15:18:08 -0000	1.27
+++ src/include/lwip/api_msg.h	19 Dec 2007 21:37:20 -0000
@@ -94,4 +94,10 @@ struct api_msg_msg {
     } jl;
 #endif /* LWIP_IGMP */
+#if LWIP_LISTEN_BACKLOG
+    struct {
+      u8_t backlog;
+    } lb;
+#endif /* LWIP_LISTEN_BACKLOG */
+
   } msg;
 };
Index: src/include/lwip/opt.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/opt.h,v
retrieving revision 1.108
diff -w -b -u -2 -p -r1.108 opt.h
--- src/include/lwip/opt.h	12 Dec 2007 19:22:20 -0000	1.108
+++ src/include/lwip/opt.h	19 Dec 2007 21:28:41 -0000
@@ -729,4 +729,11 @@
 #endif
 
+/**
+ * LWIP_LISTEN_BACKLOG: Enable the backlog option for tcp listen pcb.
+ */
+#ifndef LWIP_LISTEN_BACKLOG
+#define LWIP_LISTEN_BACKLOG             0
+#endif
+ 
 /*
    ----------------------------------
Index: src/include/lwip/tcp.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/tcp.h,v
retrieving revision 1.59
diff -w -b -u -2 -p -r1.59 tcp.h
--- src/include/lwip/tcp.h	21 Nov 2007 18:37:24 -0000	1.59
+++ src/include/lwip/tcp.h	19 Dec 2007 21:47:32 -0000
@@ -80,4 +80,9 @@ void             tcp_err     (struct tcp
 #define          tcp_sndbuf(pcb)   ((pcb)->snd_buf)
 
+#if LWIP_LISTEN_BACKLOG
+#define          tcp_accepted(pcb)      (((struct tcp_pcb_listen *)(pcb))->to_accept--)
+#define          tcp_backlog(pcb,bklog) (((struct tcp_pcb_listen *)(pcb))->backlog = bklog)
+#endif /* LWIP_LISTEN_BACKLOG */
+
 void             tcp_recved  (struct tcp_pcb *pcb, u16_t len);
 err_t            tcp_bind    (struct tcp_pcb *pcb, struct ip_addr *ipaddr,
@@ -412,4 +417,8 @@ struct tcp_pcb_listen {  
   err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
 #endif /* LWIP_CALLBACK_API */
+#if LWIP_LISTEN_BACKLOG
+  u8_t backlog;
+  u8_t to_accept;
+#endif /* LWIP_LISTEN_BACKLOG */
 };
 
