lib/connectors/hybrid/tcpsocket.js
function(socket, opts) {
  if(!(this instanceof Socket)) {
    return new Socket(socket, opts);
  }

  if(!socket || !opts) {
    throw new Error('invalid socket or opts');
  }

  if(!opts.headSize || typeof opts.headHandler !== 'function') {
    throw new Error('invalid opts.headSize or opts.headHandler');
  }

  // stream style interfaces.
  // TODO: need to port to stream2 after node 0.9
  Stream.call(this);
  this.readable = true;
  this.writeable = true;

  this._socket = socket;
  this.headSize = opts.headSize;
  this.closeMethod = opts.closeMethod;
  this.headBuffer = new Buffer(opts.headSize);
  this.headHandler = opts.headHandler;

  this.headOffset = 0;
  this.packageOffset = 0;
  this.packageSize = 0;
  this.packageBuffer = null;

  // bind event form the origin socket
  this._socket.on('data', ondata.bind(null, this));
  this._socket.on('end', onend.bind(null, this));
  this._socket.on('error', this.emit.bind(this, 'error'));
  this._socket.on('close', this.emit.bind(this, 'close'));

  this.state = ST_HEAD;
}
function(socket, opts) {
  if(!(this instanceof Socket)) {
    return new Socket(socket, opts);
  }

  if(!socket || !opts) {
    throw new Error('invalid socket or opts');
  }

  if(!opts.headSize || typeof opts.headHandler !== 'function') {
    throw new Error('invalid opts.headSize or opts.headHandler');
  }

  // stream style interfaces.
  // TODO: need to port to stream2 after node 0.9
  Stream.call(this);
  this.readable = true;
  this.writeable = true;

  this._socket = socket;
  this.headSize = opts.headSize;
  this.closeMethod = opts.closeMethod;
  this.headBuffer = new Buffer(opts.headSize);
  this.headHandler = opts.headHandler;

  this.headOffset = 0;
  this.packageOffset = 0;
  this.packageSize = 0;
  this.packageBuffer = null;

  // bind event form the origin socket
  this._socket.on('data', ondata.bind(null, this));
  this._socket.on('end', onend.bind(null, this));
  this._socket.on('error', this.emit.bind(this, 'error'));
  this._socket.on('close', this.emit.bind(this, 'close'));

  this.state = ST_HEAD;
}
function ( socket , opts ) { if ( ! ( this instanceof Socket ) ) { return new Socket ( socket , opts ) ; } if ( ! socket || ! opts ) { throw new Error ( 'invalid socket or opts' ) ; } if ( ! opts . headSize || typeof opts . headHandler !== 'function' ) { throw new Error ( 'invalid opts.headSize or opts.headHandler' ) ; } // stream style interfaces. // TODO: need to port to stream2 after node 0.9 Stream . call ( this ) ; this . readable = true ; this . writeable = true ; this . _socket = socket ; this . headSize = opts . headSize ; this . closeMethod = opts . closeMethod ; this . headBuffer = new Buffer ( opts . headSize ) ; this . headHandler = opts . headHandler ; this . headOffset = 0 ; this . packageOffset = 0 ; this . packageSize = 0 ; this . packageBuffer = null ; // bind event form the origin socket this . _socket . on ( 'data' , ondata . bind ( null , this ) ) ; this . _socket . on ( 'end' , onend . bind ( null , this ) ) ; this . _socket . on ( 'error' , this . emit . bind ( this , 'error' ) ) ; this . _socket . on ( 'close' , this . emit . bind ( this , 'close' ) ) ; this . state = ST_HEAD ; }
closed 
Tcp socket wrapper with package compositing.
Collect the package from socket and emit a completed package with 'data' event.
Uniform with ws.WebSocket interfaces.

@param {Object} socket origin socket from node.js net module
@param {Object} opts   options parameter.
opts.headSize size of package head
opts.headHandler(headBuffer) handler for package head. caculate and return body size from head data.
closed Tcp socket wrapper with package compositing . Collect the package from socket and emit a completed package with data event . Uniform with ws . WebSocket interfaces .