@@ -53,41 +53,30 @@ Connection.prototype.connect = function(port, host) {
5353 self . emit ( 'end' ) ;
5454 } ) ;
5555
56- if ( this . ssl ) {
57- this . stream . once ( 'data' , function ( buffer ) {
58- self . setBuffer ( buffer ) ;
59- var msg = self . readSslResponse ( ) ;
60- self . emit ( 'message' , msg ) ;
61- self . emit ( msg . name , msg ) ;
62- } ) ;
63- this . once ( 'sslresponse' , function ( msg ) {
64- if ( msg . text == 0x53 ) {
65- var tls = require ( 'tls' ) ;
66- self . stream . removeAllListeners ( ) ;
67- self . stream = tls . connect ( {
68- socket : self . stream ,
69- servername : host ,
70- rejectUnauthorized : self . ssl . rejectUnauthorized ,
71- ca : self . ssl . ca ,
72- pfx : self . ssl . pfx ,
73- key : self . ssl . key ,
74- passphrase : self . ssl . passphrase ,
75- cert : self . ssl . cert ,
76- NPNProtocols : self . ssl . NPNProtocols
77- } ) ;
78- self . attachListeners ( self . stream ) ;
79- self . emit ( 'sslconnect' ) ;
80- } else {
81- self . emit (
82- 'error' ,
83- new Error ( "The server doesn't support SSL/TLS connections." )
84- ) ;
85- }
86- } ) ;
87-
88- } else {
89- this . attachListeners ( this . stream ) ;
56+ if ( ! this . ssl ) {
57+ return this . attachListeners ( this . stream ) ;
9058 }
59+
60+ this . stream . once ( 'data' , function ( buffer ) {
61+ var responseCode = buffer . toString ( 'utf8' ) ;
62+ if ( responseCode != 'S' ) {
63+ return self . emit ( 'error' , new Error ( 'The server does not support SSL connections' ) ) ;
64+ }
65+ var tls = require ( 'tls' ) ;
66+ self . stream = tls . connect ( {
67+ socket : self . stream ,
68+ servername : host ,
69+ rejectUnauthorized : self . ssl . rejectUnauthorized ,
70+ ca : self . ssl . ca ,
71+ pfx : self . ssl . pfx ,
72+ key : self . ssl . key ,
73+ passphrase : self . ssl . passphrase ,
74+ cert : self . ssl . cert ,
75+ NPNProtocols : self . ssl . NPNProtocols
76+ } ) ;
77+ self . attachListeners ( self . stream ) ;
78+ self . emit ( 'sslconnect' ) ;
79+ } ) ;
9180} ;
9281
9382Connection . prototype . attachListeners = function ( stream ) {
0 commit comments