Fix errors caused by calling a null function

Don't register for the 'connect' event if there is no connectedCallback.
Correctly end the TCP connection

The previous method of simulating an incoming end and then doing and
outgoing end in the 'end' event handler, besides being convoluted and
unnecessary, causes the 'disconnect' event to be emitted twice: once for
fake 'end' event, and once when the server actually ends the connection.