only close the socket if it exists