You're trying this the wrong way round. The server should have one socket (IP-adress and port) on which it accepts connections and then it will spawn a separate thread for each client that connects. If you were to do it your way you'd need either 40 IP adresses and listening on one designated port or use one IP adress listening on 40 different ports. The former is a waste of IP adress space and the latter a pain in the arse configuration management wise as each client would have to be configured with a different port to connect to the server.
Since I cannot imagine this being a requirement I suggest you look at the ample samples available on the internet to see how a server using sockets is implemented and go by that route.
Socket server sample:
http://www.tutorialspoint.com/unix_sockets/socket_server_example.htm[
^]
Threading C Windows:
Multithreading with C and Win32 [
^]
Regards,
— Manfred