Repository: socketio/socket.io
File: lib/socket.io/client.js (JavaScript)

Code before:
    }
    this.emit('message', messages[i]);
    this.listener._onClientMessage(messages[i], this);
  }
};

Client.prototype._onConnect = function(req, res){
  var self = this
    , attachConnection = !this.connection;

  this.request = req;
  this.response = res;
  this.connection = req.connection;
	
	if(!attachConnection) attachConnection = !attachConnection && this.connection.eventsAttached === undefined;
	this.connection.eventsAttached = true;
  
  if (attachConnection){
    this.connection.addListener('end', function(){
      self._onClose();
      self.connection && self.connection.destroy()
    });
    this.connection.addListener('timeout', function(){
      self._onClose();
      self.connection && self.connection.destroy()
    });
    this.connection.addListener('error', function(){
      self._onClose();
      self.connection && self.connection.destroy()
    });
    }
  
  if (req){
    req.addListener('error', function(err){
      req.destroy && req.destroy();
    });
    req.addListener('timeout', function(err){
      req.destroy && req.destroy();
    });
    if (res){
      res.addListener('error', function(err){
        res.destroy && res.destroy();
      });
      res.addListener('timeout', function(err){
        res.destroy && res.destroy();
      });
    }
    if (this._disconnectTimeout) clearTimeout(this._disconnectTimeout);
  }
};


Review comment (refactor):
Since all three of those listeners are the same.. you could DRY it up:
    function closeAndDestroy(){ 
      self._onClose();
      self.connection && self.connection.destroy()
    }
    this.connection.addListener('end', closeAndDestroy);
    this.connection.addListener('timeout', closeAndDestroy);
    //...

And really same goes for the 4 response destroy listeners below as well. 


Code after:
    }
    this.emit('message', messages[i]);
    this.listener._onClientMessage(messages[i], this);
  }
};

Client.prototype._onConnect = function(req, res){
  var self = this
    , attachConnection = !this.connection;

  this.request = req;
  this.response = res;
  this.connection = req.connection;

  if(!attachConnection) attachConnection = !attachConnection && this.connection.eventsAttached === undefined;
  this.connection.eventsAttached = true;
  
  if (attachConnection){
    function destroyConnection(){
      self._onClose();
      self.connection && self.connection.destroy()
    };
    this.connection.addListener('end', destroyConnection);
    this.connection.addListener('timeout', destroyConnection);
    this.connection.addListener('error', destroyConnection);
    }
  
  if (req){
    function destroyRequest(){
      req.destroy && req.destroy();
    };
    req.addListener('error', destroyRequest);
    req.addListener('timeout', destroyRequest);
    if (res){
      function destroyResponse(){
        res.destroy && res.destroy();
      }
      res.addListener('error', destroyResponse);
      res.addListener('timeout', destroyResponse);
    }
    if (this._disconnectTimeout) clearTimeout(this._disconnectTimeout);
  }
};


Client.prototype._payload = function(){
  var payload = [];
  
  this.connections++;
  this.connected = true;
  this._open = true;