Close connection gracefully