close connections after exiting the main loop