implemented the main loop for the sending side