only close socket if socket exists