Today I got here nice answer that will resolve my problem. Unfortunately, I forgot to ask how about locking.
The issue was simple - in the server, each connected client will get an unique ID (reusable) from 1 to 500, which is maximum of clients.
The answer was to create a qeue and use waiting elements for new connections and just return them back when they are released.
Im not sure whether I understand correctly - also I should init the qeue with 500 elements (ints) and just take them one by one and return back once released?
If so, what about locking here, my question was mainly aimed on performance because I was using lock.