Ensure we close the socket if it already exists, so we don't exhaust the pool.

As this function is called in a loop, sockets could be exhausted quite fast if errors kept occurring. This should fix the problem.