Modify close() to ensure that the socket isn't closed until after closing the window.