Make sure socket isn't null before closing it.