Add a null check for socket