Close connection and exit when all windows closed