fix issue where sending a message to a closed connection would result in the connection not being closed correctly