Fix socket close bug