nginx-0.0.1-2003-07-21-01:15:59 import
diff --git a/src/event/modules/ngx_devpoll_module.c b/src/event/modules/ngx_devpoll_module.c
index d6f2596..e55f17c 100644
--- a/src/event/modules/ngx_devpoll_module.c
+++ b/src/event/modules/ngx_devpoll_module.c
@@ -378,7 +378,7 @@
     } else {
         if (events == 0) {
             ngx_log_error(NGX_LOG_ALERT, log, 0,
-                          "ioctl(DP_POLL) returns no events without timeout");
+                          "ioctl(DP_POLL) returned no events without timeout");
             return NGX_ERROR;
         }
 
diff --git a/src/event/modules/ngx_poll_module.c b/src/event/modules/ngx_poll_module.c
index a3a0fb1..4d50b4a 100644
--- a/src/event/modules/ngx_poll_module.c
+++ b/src/event/modules/ngx_poll_module.c
@@ -64,7 +64,7 @@
         nevents = 0;
     }
 
-    if (cycle->old_cycle == NULL 
+    if (cycle->old_cycle == NULL
         || cycle->old_cycle->connection_n < cycle->connection_n)
     {
         ngx_test_null(list,
@@ -272,7 +272,7 @@
     } else {
         if (ready == 0) {
             ngx_log_error(NGX_LOG_ALERT, log, 0,
-                          "poll() returns no events without timeout");
+                          "poll() returned no events without timeout");
             return NGX_ERROR;
         }
 
diff --git a/src/event/modules/ngx_select_module.c b/src/event/modules/ngx_select_module.c
index 3bf42ef..ff9bdbf 100644
--- a/src/event/modules/ngx_select_module.c
+++ b/src/event/modules/ngx_select_module.c
@@ -263,7 +263,11 @@
         tv.tv_sec = timer / 1000;
         tv.tv_usec = (timer % 1000) * 1000;
         tp = &tv;
+#if (HAVE_SELECT_CHANGE_TIMEOUT)
+        delta = 0;
+#else
         delta = ngx_msec();
+#endif
 
     } else {
         timer = 0;
@@ -313,8 +317,11 @@
 #endif
 
     if (timer) {
-        /* TODO: Linux returns time in tv */
+#if (HAVE_SELECT_CHANGE_TIMEOUT)
+        delta = timer - (tv.tv_sec * 1000 + tv.tv_usec / 1000);
+#else
         delta = ngx_msec() - delta;
+#endif
 
 #if (NGX_DEBUG_EVENT)
         ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ delta);
@@ -324,7 +331,7 @@
     } else {
         if (ready == 0) {
             ngx_log_error(NGX_LOG_ALERT, log, 0,
-                          "select() returns no events without timeout");
+                          "select() returned no events without timeout");
             return NGX_ERROR;
         }
 
@@ -408,7 +415,7 @@
 
     ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
 
-    /* the default FD_SETSIZE is 1024U in FreeBSD 5.x */
+    /* disable warnings: the default FD_SETSIZE is 1024U in FreeBSD 5.x */
 
     if ((unsigned) ecf->connections > FD_SETSIZE) {
         return "maximum number of connections "
diff --git a/src/event/ngx_event_accept.c b/src/event/ngx_event_accept.c
index 7b820a6..f9fdd57 100644
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -67,6 +67,7 @@
         }
 
         /* disable warnings: Win32 SOCKET is u_int while UNIX socket is int */
+
         if ((unsigned) s >= (unsigned) ecf->connections) {
 
             ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
@@ -183,7 +184,7 @@
         if (c->log == NULL) {
             return;
         }
-        ngx_memcpy(c->log, ev->log, sizeof(ngx_log_t));
+        ngx_memcpy(c->log, ls->log, sizeof(ngx_log_t));
         rev->log = wev->log = c->log;
 
         /* TODO: x86: MT: lock xadd, MP: lock xadd, shared */
diff --git a/src/event/ngx_event_connect.c b/src/event/ngx_event_connect.c
index ca8a4a3..8cd01d3 100644
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -4,16 +4,21 @@
 
 int ngx_event_connect_peer(ngx_connect_peer_t *cp)
 {
+    time_t   now;
 
+    /* TODO: cached connection */
+
+    now = ngx_time();
 
     if (cp->peers->number > 1) {
 
-        /* it's a first try - get current peer */
+        /* there are several peers */
 
         if (cp->tries == cp->peers->number) {
 
-            /* Here is the race condition
-               when the peers are shared between
+            /* it's a first try - get a current peer */
+
+            /* Here is the race condition when the peers are shared between
                the threads or the processes but it should not be serious */
 
             cp->cur_peer = cp->peers->current++;
@@ -22,10 +27,11 @@
                 cp->peers->current = 0;
             }
 
-            /* */
+            /* the end of the race condition */
 
 #if (NGX_MULTITHREADED || NGX_MULTIPROCESSED)
             /* eliminate the sequences of the race condition */
+
             if (cp->cur_peer >= cp->peers->number) {
                 cp->cur_peer = 0;
             }
@@ -34,11 +40,12 @@
 
         if (cp->peers->max_fails > 0) {
 
+            /* the peers support a fault tolerance */
+
             for ( ;; ) {
                 peer = &cp->peers->peers[cp->cur_peer];
 
-                /* Here is the race condition
-                   when the peers are shared between
+                /* Here is the race condition when the peers are shared between
                    the threads or the processes but it should not be serious */
 
                 if (peer->fails <= cp->peers->max_fails
@@ -47,7 +54,7 @@
                     break;
                 }
 
-                /* */
+                /* the end of the race condition */
 
                 cp->cur_peer++;
 
@@ -71,23 +78,23 @@
     s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0);
 
     if (s == -1) {
-        ngx_log_error(NGX_LOG_ALERT, cn->log, ngx_socket_errno,
+        ngx_log_error(NGX_LOG_ALERT, cp->log, ngx_socket_errno,
                       ngx_socket_n " failed");
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+        return NGX_ERROR;
     }
 
-    if (cn->rcvbuf) {
+    if (cp->rcvbuf) {
         if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
-                       (const void *) &cn->rcvbuf, sizeof(int)) == -1) {
-            ngx_log_error(NGX_LOG_ALERT, cn->log, ngx_socket_errno,
+                       (const void *) &cp->rcvbuf, sizeof(int)) == -1) {
+            ngx_log_error(NGX_LOG_ALERT, cp->log, ngx_socket_errno,
                           "setsockopt(SO_RCVBUF) failed");
 
             if (ngx_close_socket(s) == -1) {
-                ngx_log_error(NGX_LOG_ALERT, cn->log, ngx_socket_errno,
+                ngx_log_error(NGX_LOG_ALERT, cp->log, ngx_socket_errno,
                               ngx_close_socket_n " failed");
             }
 
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
+            return NGX_ERROR;
         }
     }
 
@@ -100,12 +107,33 @@
                           ngx_close_socket_n " failed");
         }
 
-        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+        return NGX_ERROR;
     }
 
-    c = &ngx_connections[s];
-    rev = &ngx_read_events[s];
-    wev = &ngx_write_events[s];
+#if (WIN32)
+        /*
+         * Winsock assignes a socket number divisible by 4
+         * so to find a connection we divide a socket number by 4.
+         */
+
+        if (s % 4) {
+            ngx_log_error(NGX_LOG_EMERG, cp->log, 0,
+                          ngx_socket_n
+                          " created socket %d, not divisible by 4", s);
+            exit(1);
+        }
+
+    c = &ngx_cycle->connections[s / 4];
+    rev = &ngx_cycle->read_events[s / 4];
+    wev = &ngx_cycle->write_events[s / 4];
+
+#else
+
+    c = &ngx_cycle->connections[s];
+    rev = &ngx_cycle->read_events[s];
+    wev = &ngx_cycle->write_events[s];
+
+#endif
 
     instance = rev->instance;
 
@@ -120,7 +148,9 @@
 
     rev->instance = wev->instance = !instance;
 
+    !!!!!!!!!!!!!!!
+
     rev->log = wev->log = c->log = cn->log;
     c->fd = s;
     wev->close_handler = rev->close_handler = ngx_event_close_connection;
-
+}
diff --git a/src/event/ngx_event_connect.h b/src/event/ngx_event_connect.h
index ecfedf9..afd4a54 100644
--- a/src/event/ngx_event_connect.h
+++ b/src/event/ngx_event_connect.h
@@ -3,10 +3,8 @@
 
 
 #include <ngx_config.h>
-#include <ngx_string.h>
-#include <ngx_log.h>
+#include <ngx_core.h>
 #include <ngx_event.h>
-#include <ngx_connection.h>
 
 
 typedef struct {
@@ -37,6 +35,8 @@
     ngx_peers_t       *peers;
     int                cur_peer;
     int                tries;
+
+    unsigned           cached:1;
 } ngx_connect_peer_t;
 
 
diff --git a/src/event/ngx_event_timer.c b/src/event/ngx_event_timer.c
index e6bec42..b70bafb 100644
--- a/src/event/ngx_event_timer.c
+++ b/src/event/ngx_event_timer.c
@@ -4,6 +4,10 @@
 #include <ngx_event.h>
 
 
+/* in multithreaded enviroment all timer operations must be
+   protected by the single mutex */
+
+
 static ngx_event_t  *ngx_timer_queue, ngx_temp_timer_queue;
 static int           ngx_timer_cur_queue;
 static int           ngx_timer_queue_num;