Properly deal with connection closing, close #2157