THRIFT-3786: Node TLS emits 'connect' before connection is ready
Client: Node
Patch: James Reggio

This closes #985
commit b76bad70826d539ddfb26b5277ff3bc09ffa9803
Author: James Reggio <james.reggio@gmail.com>
Date: 2016-04-11T17:53:12Z
THRIFT-3786 Fix premature firing of `connect` event for secure sockets
diff --git a/lib/nodejs/lib/thrift/connection.js b/lib/nodejs/lib/thrift/connection.js
index 4b84b76..f9c9bcc 100644
--- a/lib/nodejs/lib/thrift/connection.js
+++ b/lib/nodejs/lib/thrift/connection.js
@@ -36,6 +36,7 @@
 
   this.seqId2Service = {};
   this.connection = stream;
+  this.ssl = (stream.encrypted);
   this.options = options || {};
   this.transport = this.options.transport || TBufferedTransport;
   this.protocol = this.options.protocol || TBinaryProtocol;
@@ -61,7 +62,8 @@
       this.options.connect_timeout > 0) {
      this.connect_timeout = +this.options.connect_timeout;
   }
-  this.connection.addListener("connect", function() {
+
+  this.connection.addListener(this.ssl ? "secureConnect" : "connect", function() {
     self.connected = true;
 
     this.setTimeout(self.options.timeout || 0);
@@ -78,24 +80,6 @@
     self.emit("connect");
   });
 
-  this.connection.addListener("secureConnect", function() {
-    self.connected = true;
-
-    this.setTimeout(self.options.timeout || 0);
-    this.setNoDelay();
-    this.frameLeft = 0;
-    this.framePos = 0;
-    this.frame = null;
-    self.initialize_retry_vars();
-
-    self.offline_queue.forEach(function(data) {
-      self.connection.write(data);
-    });
-
-    self.emit("connect");
-  });
-
-
   this.connection.addListener("error", function(err) {
     // Only emit the error if no-one else is listening on the connection
     // or if someone is listening on us