When a client initiates a disconnection from server, there is an exchange between client and server to inform about the state of disconnection. When the process is finished, the client socket connection states as TIME_WAIT during a variable time. This is a normal behavior, in windows operating systems, this time defaults to about 4 minutes.
You can reduce or eliminate this behavior, do with careful, using the following alternatives.
Regedit
You can reduce the TIME_WAIT value using the Windows Regedit
1. Open Regedit and access to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ Services\TCPIP\Parameters registry subkeys.
2. Create a new REG_DWORD value named TcpTimedWaitDelay
3. Set the value in Seconds. Example: if you set a value of 5, means that TIME_WAIT will waits as max as 5 seconds.
4. Save and restart the system.
LINGER
Another option to avoid TIME_WAIT state, is use the socket option SO_LINGER, if enabled, instead of closing the connection gracefully, the client resets the connection so the TIME_WAIT state is avoided.
You can enable this option using LingerState property, by default has a value of -1. If you set a value of zero, the connection will be reset when disconnecting from socket without Timeout.
This options is probably the less recommended and only use as a last option.