Open connection only after it's been added to the connections array.
Defer closing the native socket slightly, in an attempt to ensure that the stream objects are properly cleaned up.
Take reference to input/output streams at top of routine, as they may be cleared out whilst it's running.