Close socket properly on exit