Send pending messages before closing socket