correctly wait for the server to close