Ensure socket is properly closed