diff --git a/lib/_http_client.js b/lib/_http_client.js index e490dce5d821d6..8ddf3341cf98c7 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -504,21 +504,23 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) { // in the future (when a socket gets assigned out of the pool and is // eventually writable). var self = this; + + function callSocketMethod() { + if (method) + self.socket[method].apply(self.socket, arguments_); + + if (typeof cb === 'function') + cb(); + } + var onSocket = function() { if (self.socket.writable) { - if (method) { - self.socket[method].apply(self.socket, arguments_); - } - if (cb) { cb(); } + callSocketMethod(); } else { - self.socket.once('connect', function() { - if (method) { - self.socket[method].apply(self.socket, arguments_); - } - if (cb) { cb(); } - }); + self.socket.once('connect', callSocketMethod); } }; + if (!self.socket) { self.once('socket', onSocket); } else {