{
        int rc ;
        Operation* new_op = NULL;
-       ber_socket_t s = (ber_socket_t)argv;
+       ber_socket_t s = (long)argv;
 
        /*
         * read incoming LDAP requests. If there is more than one,
        if( ( rc = connection_read( s, &new_op ) ) < 0 ) {
                Debug( LDAP_DEBUG_TRACE, "connection_read(%d) error\n", s, 0, 0 );
                tcp_close( s );
-               return (void*)rc;
+               return (void*)(long)rc;
        }
 
        /* execute the queued request in the same thread */
        if( new_op ) {
-               rc = (int)connection_operation(
+               rc = (long)connection_operation(
                        ldap_pvt_thread_pool_context(), new_op );
        }
 
-       return (void*)rc;
+       return (void*)(long)rc;
 }
 
 int connection_read_activate( ber_socket_t s )
        slapd_clr_read( s, 0 );
 
        rc = ldap_pvt_thread_pool_submit( &connection_pool,
-               connection_read_thread, (void *) s );
+               connection_read_thread, (void *)(long)s );
 
        if( rc != 0 ) {
                Debug( LDAP_DEBUG_ANY,
 }
 
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
+static int connection_write( ber_socket_t s );
+static void *connection_write_thread( void *ctx, void *arg )
+{
+       return (void *)(long)connection_write((long)arg);
+}
+
 int connection_write_activate( ber_socket_t s )
 {
        int rc;
         * thread write data on it. Otherwise the listener thread will repeatedly
         * submit the same event on it to the pool.
         */
-
-#ifndef SLAP_LIGHTWEIGHT_DISPATCHER
        slapd_clr_write( s, 0);
-       c->c_n_write++;
-#endif
 
        rc = ldap_pvt_thread_pool_submit( &connection_pool,
-               connection_read_thread, (void *) s );
+               connection_write_thread, (void *)(long)s );
 
        if( rc != 0 ) {
                Debug( LDAP_DEBUG_ANY,
-                       "connection_write_activiate(%d): submit failed (%d)\n",
+                       "connection_write_activate(%d): submit failed (%d)\n",
                        (int) s, rc, 0 );
        }
        return rc;
 
 #ifndef SLAP_LIGHTWEIGHT_DISPATCHER
        slapd_clr_write( s, 0);
-       c->c_n_write++;
 #endif
+       c->c_n_write++;
 
        Debug( LDAP_DEBUG_TRACE,
                "connection_write(%d): waking output for id=%lu\n",
 
                        }
                }
 #endif
-
                at = 0;
 
                ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
                nwriters = slap_daemon.sd_nwriters;
-               SLAP_EVENT_INIT;
 
                for ( l = 0; slap_listeners[l] != NULL; l++ ) {
                        Listener *lr = slap_listeners[l];
                        }
                }
 
+               SLAP_EVENT_INIT;
+
                nfds = SLAP_EVENT_MAX;
 
                if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
                        /*
                         * NOTE: it is possible that the connection was closed
                         * and that the stream is now inactive.
-                        * connection_write() must validitate the stream is still
+                        * connection_write() must validate the stream is still
                         * active.
                         */
                        if ( connection_write( wd ) < 0 ) {