handling connection close properly