A new connection is established when the
method called for the listening socket returns. The connection is closed when closing the socket returned by the
call. So you have to change the counter at those points in your code.
socket client_socket = server_socket.accept();
If a server has to support multiple clients connected at the same time, it has to use an own thread for each connection. Such is handled by most socket tutorials like Introducing Threads in Socket Programming in Java - GeeksforGeeks