cleanup connection closing code