ensure that the connection is closed when neccessary