You need to have two while loops inside another while loop. The first will loop until it can open a connection and the second will loop as long as the communication works without errors. We use a small buffer for the errors and that way, if there is only one error, the connection isn't terminated.
Also, try looking for "TCP" on the site and see what you can find.
___________________
Try to take over the world!