Fix potential resource leak caused by missing socket close statement