Close connection when script is terminated