_connectSocket()

in src/internal/ClientSocket.ts [201:236]


    _connectSocket(handshakeRequest) {
        const onConnected = async () => {
            this._state = STATE.HANDSHAKE;
            // send handshake
            await this._sendRequest(handshakeRequest);
        };

        const options: (NetConnectOpts | ConnectionOptions) = Object.assign({},
            this._config.options,
            { host : this._host, port : this._port, version : this._version });

        if (this._config.useTLS) {
            this._socket = tls.connect(<ConnectionOptions>options, onConnected);
        }
        else {
            this._socket = net.createConnection(<NetConnectOpts>options, onConnected);
        }

        this._socket.on('data', async (data: Buffer) => {
            try {
                await this._processResponse(data);
            }
            catch (err) {
                this._error = err.message;
                this._disconnect();
            }
        });
        this._socket.on('close', () => {
            this._disconnect(false);
        });
        this._socket.on('error', (error) => {
            this._error = this._state === STATE.INITIAL ?
                'Connection failed: ' + error : error;
            this._disconnect();
        });
    }