diff -r da839141cc0e memcached.c
--- a/memcached.c	Fri Feb 01 17:06:11 2008 -0800
+++ b/memcached.c	Sat Feb 02 12:53:34 2008 -0800
@@ -64,7 +64,7 @@ std *
  */
 static void drive_machine(conn *c);
 static int new_socket(struct addrinfo *ai);
-static int server_socket(const int port, const bool is_udp);
+static int *server_socket(const int port, const bool is_udp);
 static int try_read_command(conn *c);
 static int try_read_network(conn *c);
 static int try_read_udp(conn *c);
@@ -1959,20 +1959,25 @@ static bool update_event(conn *c, const 
  * Sets whether we are listening for new connections or not.
  */
 void accept_new_conns(const bool do_accept) {
+    conn *next;
+
     if (! is_listen_thread())
         return;
-    if (do_accept) {
-        update_event(listen_conn, EV_READ | EV_PERSIST);
-        if (listen(listen_conn->sfd, 1024) != 0) {
-            perror("listen");
-        }
-    }
-    else {
-        update_event(listen_conn, 0);
-        if (listen(listen_conn->sfd, 0) != 0) {
-            perror("listen");
-        }
-    }
+
+    for (next = listen_conn; next; next = next->next) {
+        if (do_accept) {
+            update_event(next, EV_READ | EV_PERSIST);
+            if (listen(next->sfd, 1024) != 0) {
+                perror("listen");
+            }
+        }
+        else {
+            update_event(next, 0);
+            if (listen(next->sfd, 0) != 0) {
+                perror("listen");
+            }
+        }
+  }
 }
 
 
@@ -2342,13 +2347,17 @@ static void maximize_sndbuf(const int sf
 }
 
 
-static int server_socket(const int port, const bool is_udp) {
+static int *server_socket(const int port, const bool is_udp) {
     int sfd;
+    int *sfd_list;
+    int *sfd_ptr;
     struct linger ling = {0, 0};
     struct addrinfo *ai;
+    struct addrinfo *next;
     struct addrinfo hints;
     char port_buf[NI_MAXSERV];
     int error;
+    int count;
 
     int flags =1;
 
@@ -2377,39 +2386,50 @@ static int server_socket(const int port,
       else
         perror("getaddrinfo()");
 
-      return -1;
-    }
-
-    if ((sfd = new_socket(ai)) == -1) {
-        freeaddrinfo(ai);
-        return -1;
-    }
-
-    setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags));
-    if (is_udp) {
-        maximize_sndbuf(sfd);
-    } else {
-        setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags));
-        setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
-        setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
-    }
-
-    if (bind(sfd, ai->ai_addr, ai->ai_addrlen) == -1) {
-        perror("bind()");
-        close(sfd);
-        freeaddrinfo(ai);
-        return -1;
-    }
-    if (!is_udp && listen(sfd, 1024) == -1) {
-        perror("listen()");
-        close(sfd);
-        freeaddrinfo(ai);
-        return -1;
+      return NULL;
+    }
+
+    for (count= 1, next= ai; next->ai_next; next= next->ai_next, count++);
+
+    sfd_list= (int *)malloc(sizeof(int) * MIN_NUMBER_OF_SOCKETS * count);
+    memset(sfd_list, 0, sizeof(int) * MIN_NUMBER_OF_SOCKETS * count);
+
+    for (sfd_ptr= sfd_list, next= ai; next->ai_next; next= next->ai_next, sfd_ptr++) {
+        if ((sfd = new_socket(next)) == -1) {
+            free(sfd_list);
+            freeaddrinfo(ai);
+            return NULL;
+        }
+
+        setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags));
+        if (is_udp) {
+            maximize_sndbuf(sfd);
+        } else {
+            setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags));
+            setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
+            setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
+        }
+
+        if (bind(sfd, next->ai_addr, next->ai_addrlen) == -1) {
+            perror("bind()");
+            close(sfd);
+            freeaddrinfo(ai);
+            free(sfd_list);
+            return NULL;
+        }
+        if (!is_udp && listen(sfd, 1024) == -1) {
+            perror("listen()");
+            close(sfd);
+            freeaddrinfo(ai);
+            free(sfd_list);
+            return NULL;
+        }
+        *sfd_ptr= sfd;
     }
 
     freeaddrinfo(ai);
 
-    return sfd;
+    return sfd_list;
 }
 
 static int new_socket_unix(void) {
@@ -2430,8 +2450,9 @@ static int new_socket_unix(void) {
     return sfd;
 }
 
-static int server_socket_unix(const char *path, int access_mask) {
+static int *server_socket_unix(const char *path, int access_mask) {
     int sfd;
+    int *sfd_list;
     struct linger ling = {0, 0};
     struct sockaddr_un addr;
     struct stat tstat;
@@ -2439,12 +2460,15 @@ static int server_socket_unix(const char
     int old_umask;
 
     if (!path) {
-        return -1;
+        return NULL;
     }
 
     if ((sfd = new_socket_unix()) == -1) {
-        return -1;
-    }
+        return NULL;
+    }
+
+    sfd_list= (int *)malloc(sizeof(int) * 2);
+    memset(sfd_list, 0, sizeof(int) * 2);
 
     /*
      * Clean up a previous socket file if we left it around
@@ -2471,28 +2495,40 @@ static int server_socket_unix(const char
         perror("bind()");
         close(sfd);
         umask(old_umask);
-        return -1;
+        return NULL;
     }
     umask(old_umask);
     if (listen(sfd, 1024) == -1) {
         perror("listen()");
         close(sfd);
-        return -1;
-    }
-    return sfd;
+        return NULL;
+    }
+
+    *sfd_list= sfd;
+
+    return sfd_list;
 }
 
 /* listening socket */
-static int l_socket = 0;
+static int *l_socket = NULL;
 
 /* udp socket */
-static int u_socket = -1;
+static int *u_socket = NULL;
 
 /* invoke right before gdb is called, on assert */
 void pre_gdb(void) {
     int i;
-    if (l_socket > -1) close(l_socket);
-    if (u_socket > -1) close(u_socket);
+    int *ptr;
+    if (l_socket) {
+        for (ptr= l_socket; *ptr; ptr++) {
+          if (*ptr > -1) close(*ptr);
+        }
+    }
+    if (u_socket) {
+        for (ptr= u_socket; *ptr; ptr++)  {
+          if (*ptr > -1) close(*ptr);
+        }
+    }
     for (i = 3; i <= 500; i++) close(i); /* so lame */
     kill(getpid(), SIGABRT);
 }
@@ -2869,7 +2905,7 @@ int main (int argc, char **argv) {
     /* create the listening socket and bind it */
     if (settings.socketpath == NULL) {
         l_socket = server_socket(settings.port, 0);
-        if (l_socket == -1) {
+        if (l_socket == NULL) {
             fprintf(stderr, "failed to listen\n");
             exit(EXIT_FAILURE);
         }
@@ -2878,7 +2914,7 @@ int main (int argc, char **argv) {
     if (settings.udpport > 0 && settings.socketpath == NULL) {
         /* create the UDP listening socket and bind it */
         u_socket = server_socket(settings.udpport, 1);
-        if (u_socket == -1) {
+        if (u_socket == NULL) {
             fprintf(stderr, "failed to listen on UDP port %d\n", settings.udpport);
             exit(EXIT_FAILURE);
         }
@@ -2903,9 +2939,9 @@ int main (int argc, char **argv) {
     /* create unix mode sockets after dropping privileges */
     if (settings.socketpath != NULL) {
         l_socket = server_socket_unix(settings.socketpath,settings.access);
-        if (l_socket == -1) {
-            fprintf(stderr, "failed to listen\n");
-            exit(EXIT_FAILURE);
+        if (l_socket == NULL) {
+          fprintf(stderr, "failed to listen\n");
+          exit(EXIT_FAILURE);
         }
     }
 
@@ -2964,10 +3000,21 @@ int main (int argc, char **argv) {
         exit(EXIT_FAILURE);
     }
     /* create the initial listening connection */
-    if (!(listen_conn = conn_new(l_socket, conn_listening,
-                                 EV_READ | EV_PERSIST, 1, false, main_base))) {
-        fprintf(stderr, "failed to create listening connection");
-        exit(EXIT_FAILURE);
+    int *l_socket_ptr;
+    for (l_socket_ptr= l_socket; *l_socket_ptr; l_socket_ptr++) {
+        conn *listen_conn_add, *next;
+        next = NULL;
+        if (!(listen_conn_add = conn_new(*l_socket_ptr, conn_listening,
+                                         EV_READ | EV_PERSIST, 1, false, main_base))) {
+            fprintf(stderr, "failed to create listening connection");
+            exit(EXIT_FAILURE);
+        }
+
+        if (listen_conn == NULL) {
+            next = listen_conn = listen_conn_add;
+        } else {
+            next->next= listen_conn_add;
+        }
     }
     /* start up worker threads if MT mode */
     thread_init(settings.num_threads, main_base);
@@ -2986,10 +3033,10 @@ int main (int argc, char **argv) {
     }
     delete_handler(0, 0, 0); /* sets up the event */
     /* create the initial listening udp connection, monitored on all threads */
-    if (u_socket > -1) {
+    if (u_socket) {
         for (c = 0; c < settings.num_threads; c++) {
             /* this is guaranteed to hit all threads because we round-robin */
-            dispatch_conn_new(u_socket, conn_read, EV_READ | EV_PERSIST,
+            dispatch_conn_new(*u_socket, conn_read, EV_READ | EV_PERSIST,
                               UDP_READ_BUFFER_SIZE, 1);
         }
     }
diff -r da839141cc0e memcached.h
--- a/memcached.h	Fri Feb 01 17:06:11 2008 -0800
+++ b/memcached.h	Sat Feb 02 12:53:34 2008 -0800
@@ -38,6 +38,9 @@
 #define ITEM_LIST_HIGHWAT 400
 #define IOV_LIST_HIGHWAT 600
 #define MSG_LIST_HIGHWAT 100
+
+/* Minimum number of sockets to allocate for listeners */
+#define MIN_NUMBER_OF_SOCKETS 2
 
 /* Get a consistent bool type */
 #if HAVE_STDBOOL_H
@@ -150,7 +153,8 @@ enum conn_states {
 #define NREAD_PREPEND 5
 #define NREAD_CAS 6
 
-typedef struct {
+typedef struct conn conn;
+struct conn {
     int    sfd;
     int    state;
     struct event event;
@@ -219,7 +223,8 @@ typedef struct {
     int    bucket;    /* bucket number for the next command, if running as
                          a managed instance. -1 (_not_ 0) means invalid. */
     int    gen;       /* generation requested for the bucket */
-} conn;
+    conn   *next;     /* Used for generating a list of conn structures */
+};
 
 /* number of virtual buckets for a managed instance */
 #define MAX_BUCKETS 32768
