Detect when server disconnects properly, so we avoid infinite loop