Updated code to reflect comment. At this point the callback must be called with an error to avoid writing to the socket before it is connected