diff --git a/lib/jsftp.js b/lib/jsftp.js index d48c2de..c34a0f5 100644 --- a/lib/jsftp.js +++ b/lib/jsftp.js @@ -115,6 +115,8 @@ Ftp.prototype.reemit = function(event) { }; Ftp.prototype._createSocket = function(port, host, firstAction) { + var self = this; + if (this.socket && this.socket.destroy) { this.socket.destroy(); } @@ -125,13 +127,15 @@ Ftp.prototype._createSocket = function(port, host, firstAction) { this.resParser = new ResponseParser(); this.authenticated = false; - this.socket = Net.createConnection(port, host, firstAction || NOOP); + this.socket = Net.createConnection(port, host, firstAction || NOOP) + .on('error', (err) => self.emit('error', err)); + + this.socket.setTimeout(self.timeout || TIMEOUT); this.socket.on('connect', this.reemit('connect')); this.socket.on('timeout', this.reemit('timeout')); this.pipeline = combine(this.socket, this.resParser); - var self = this; this.pipeline.on('data', function(data) { self.emit('data', data);