// stage 0

        a=inet_ntoa( work->addr.sin_addr);
        if ( 0==(rc=getaddrinfo( a, NULL, &hint, &ai )) )
        {
            strcpy( hostname, ai->ai_canonname );
            freeaddrinfo( ai );
        }
        else
        {
            strcpy( hostname, a );
            log_error("[%02d] warning: unable to determine hostname of %s, error %d", c->id, a, rc);
        }

        // there is a possibility that the client has gone away because we took to long
        // to get started on the connection - work was queued for too long.
        if ( -1==fstat( work->soc, &s ) || !S_ISSOCK(s.st_mode) )
        {
            log_error( "\x1b[1m[%02d] client %s [%s:%d] went away (%lu seconds wait), skipping\x1b[0m\n", 
            c->id, hostname, a, ntohs(work->addr.sin_port), *(c->t)-work->t );
            free(work);
            continue;
        }


        log_error( "\x1b[1m[%02u.%04u] connection %s [%s:%d] accepted.\x1b[0m\n", c->id, work->id, hostname, a, ntohs(work->addr.sin_port) );
        
        // check statsu of our connection to right hand side, if not there, open it.
        // we'll need a timeout setting? 
        // to reuse a connection, we send a RSET instead of QUIT, and start over for the next
        // inbound client request. On timeout we send a QUIT to right.
        
        if ( -1==(right=socket(AF_INET, SOCK_STREAM, getprotobyname("tcp")->p_proto )) )
        {
            log_error("\x1b[1m[%02d] unable to create socket for right side.\x1b[0m\n", c->id );
            //writefputs( "437 blah\r\n", work->soc );
            close(work->soc);
            free(work);
            continue;
        }

        memset( &addr_right, 0, sizeof(addr_right));

        inet_aton( "127.0.0.1", &addr_right.sin_addr );
        addr_right.sin_family=AF_INET;
        addr_right.sin_port=htons(10025);

        if( 0!=connect( right, (struct sockaddr*)&addr_right, sizeof(addr_right)) )
        {
            log_error( "[%02d] connect() failed; error %d: %s\n", c->id, errno, strerror(errno) );
            close(work->soc);
            free(work);
            continue;
        }
        log_error( "\x1b[1m[%02u.%04u] connected to %s [%s:%d]\x1b[0m\n", c->id, work->id, "localhost", "127.0.0.1", 10025 );
        
        //log_error( "\x1b[%d;1m[%02u.%04u] proxying [%s:%d] to [%s:%d]\x1b[0;37m\n", logcolor[c->id], c->id, work->id, 
        //a, ntohs(work->addr.sin_port), "127.0.0.1", 10025 );
        
        right2=dup(right);
        fcntl( right, F_SETFL, O_NONBLOCK );
                
        if ( NULL==(right_out=fdopen( right2, "w")) )
        {
            log_error("unable to open right_out stream; error %d: %s\n", errno, strerror(errno) );
        }
        
        if ( NULL==(right_in=fdopen( right, "r")) )
        {
            log_error("unable to open right_in stream; error %d: %s\n", errno, strerror(errno) );
        }

        left=work->soc;
        left2=dup(left);
        fcntl( left, F_SETFL, O_NONBLOCK );
        
        if ( NULL==(left_in=fdopen( left, "r" )) )
        {
            log_error("unable to open smtp_in stream; error %d: %s\n", errno, strerror(errno) );
            free(work);
            continue;
        }
        
        if ( NULL==(left_out=fdopen( left2, "w")) )
        {
            log_error("unable to open smtp_out stream; error %d: %s\n", errno, strerror(errno) );
            fclose(left_in);
            free(work);
            continue;
        }

        
    stage=1;
