Only close connection when processing is done