close connection when done with loop