lib/server.js
function connection(socket, server) {
  var conn = new Connection(socket)
    , tcp = (server instanceof TcpServer);

  var maxclients = this.conf.get(ConfigKey.MAXCLIENTS);
  if(maxclients && (this.state.length + 1 > maxclients)) {

    this.state.emit('rejected', conn.client.addr, maxclients);

    this.log.debug('rejected %s, exceeds maxclients %s',
      conn.client.addr, maxclients);

    function onWrite() {
      socket.end();
    }

    return conn.write(MaxClients, onWrite.bind(this));
  }

  if(tcp) {
    this.log.debug('accepted connection from %s', conn.client.addr);
  }else if(server) {
    this.log.debug('accepted connection to %s', server.name);
  }else if(!server) {
    this.log.debug('accepted connection in %s', process.pid);
  }

  var timeout = this.conf.get(ConfigKey.TIMEOUT);
  if(typeof timeout === 'number' && timeout) {
    this.log.debug('setting idle timeout for client #%s to %ss',
      conn.id, timeout);
    // config is seconds, socket want milliseconds
    socket.setTimeout(timeout * 1000, function onTimeout() {
      // TODO: log client connection timeout
      socket.destroy();
    })
  }

  conn.once('disconnect', this.disconnect.bind(this));
  conn.on('command', this.command);
  this._state.addConnection(conn);
}
function connection(socket, server) {
  var conn = new Connection(socket)
    , tcp = (server instanceof TcpServer);

  var maxclients = this.conf.get(ConfigKey.MAXCLIENTS);
  if(maxclients && (this.state.length + 1 > maxclients)) {

    this.state.emit('rejected', conn.client.addr, maxclients);

    this.log.debug('rejected %s, exceeds maxclients %s',
      conn.client.addr, maxclients);

    function onWrite() {
      socket.end();
    }

    return conn.write(MaxClients, onWrite.bind(this));
  }

  if(tcp) {
    this.log.debug('accepted connection from %s', conn.client.addr);
  }else if(server) {
    this.log.debug('accepted connection to %s', server.name);
  }else if(!server) {
    this.log.debug('accepted connection in %s', process.pid);
  }

  var timeout = this.conf.get(ConfigKey.TIMEOUT);
  if(typeof timeout === 'number' && timeout) {
    this.log.debug('setting idle timeout for client #%s to %ss',
      conn.id, timeout);
    // config is seconds, socket want milliseconds
    socket.setTimeout(timeout * 1000, function onTimeout() {
      // TODO: log client connection timeout
      socket.destroy();
    })
  }

  conn.once('disconnect', this.disconnect.bind(this));
  conn.on('command', this.command);
  this._state.addConnection(conn);
}
function connection ( socket , server ) { var conn = new Connection ( socket ) , tcp = ( server instanceof TcpServer ) ; var maxclients = this . conf . get ( ConfigKey . MAXCLIENTS ) ; if ( maxclients && ( this . state . length + 1 > maxclients ) ) { this . state . emit ( 'rejected' , conn . client . addr , maxclients ) ; this . log . debug ( 'rejected %s, exceeds maxclients %s' , conn . client . addr , maxclients ) ; function onWrite ( ) { socket . end ( ) ; } return conn . write ( MaxClients , onWrite . bind ( this ) ) ; } if ( tcp ) { this . log . debug ( 'accepted connection from %s' , conn . client . addr ) ; } else if ( server ) { this . log . debug ( 'accepted connection to %s' , server . name ) ; } else if ( ! server ) { this . log . debug ( 'accepted connection in %s' , process . pid ) ; } var timeout = this . conf . get ( ConfigKey . TIMEOUT ) ; if ( typeof timeout === 'number' && timeout ) { this . log . debug ( 'setting idle timeout for client #%s to %ss' , conn . id , timeout ) ; // config is seconds, socket want milliseconds socket . setTimeout ( timeout * 1000 , function onTimeout ( ) { // TODO: log client connection timeout socket . destroy ( ) ; } ) } conn . once ( 'disconnect' , this . disconnect . bind ( this ) ) ; conn . on ( 'command' , this . command ) ; this . _state . addConnection ( conn ) ; }
Handle a connection event.

@param socket The underlying client socket.
@param server The TCP or UNIX server.
Handle a connection event .