got rid of hardcoded remaining_length in connect_to_broker