I'm writing some lines of code to implement a two phased protocol that should work as follows:

1) in the first phase i have my server waiting for clients to connect. I know the number of total clients available in a static variable and I set a first timeout. In this time all my clients must connect to the server. If the time interval finishes without all clients connected, I decide for a global abort to perform in the second phase. In the other case, if all clients are connected, i decide for a global commit.

2) in the second phase I send to all the clients connected my global decision and wait for a response from each of them: If I receive responses from all the clients, the protocol finishes successfully, otherwise, I must repeat the second phase untill I receive responses from all the clients.

In my application, as a client attempts to connect I create a new Thread. Where can I find some help to manage sockets and threads unite?
Thank you very much.