Fixed NullPointerException on client close

A NullPointerException would occur on client close when the client isn't
connected to a server. Added checks for null values.