Close socket if TCP connecting fails to avoid leaking sockets