How to protect Modbus TCPIP server from connecting more than Max number of connections, new connection causing dropping of old connections

Hi I have implemented modbus server which has limit to max 5 client connections but if test it for connecting more than 5 it will drops or close old connection and connect with new connection here the physically line is of 1st connection only but it will get new PID as i am creating new child with fork parent process and reusing socket address.

Expectation is it should not allow to connect more than 5 connections and should not disconnect first 5 connection unless and until they want to disconnect or any error occur.
Well, to be blunt, if you're implementing this yourself, the behavior is going to be exactly what you coded it to do (after all, computers only do what you tell them to do). It seems currently, you've implemented it to unconditionally reuse sockets, thus dropping a previously established socket to establish a new one.

You need to keep track (using a status array, or similar) of all of the currently active connections in your parent process. When a new connection comes in, instead of unconditionally creating a new child process, you must instead check if all of your connections are currently in use, and if so, simply close the new connection instead of starting a new child process.

Your steps should be roughly as follows:
  1. Create and open your listening socket (on TCP port 502) in your parent process (for example, using the socket() and bind() functions).
  2. Next, mark the socket as a passive, listening socket to be used to accept incoming connection requests (for example, using the listen() function and passing the number 5 as your maximum allowed backlog).
  3. Then wait to receive and accept a new connection on your listening socket (for example, using the accept() function).
  4. When a new connection is received, check the connection status array that you've created, looking for a free slot. If there are no free slots left, then simply close the new connection immediately (for example, using the close() function). But if a free slot is found, spawn a new child thread for the connection and mark that slot as in use in your connection status array (similarly, your child process will need to mark the slot as free after it, or the client, closes the connection).