close socket safely when disconnect