CLOSE_WAIT generally means that the remote end of a TCP connection terminated the link but the local end (the server) has received a FIN but has not yet closed the connection. If you call shutdown() without calling close(), this can happen as well. The TCP/IP stack cleans this up, generally when a TTL expires or when all required closes and terminations happen. If you do not perform a read() or recv() on the socket, then it will remain open until the stack detects that the remote is gone - gone could mean a close() and shutdown() or unplugging the cable or disconnecting the WiFi. This part is only partially in your applications control. You can try reducing the TTL, which may increase network traffic. You can try increasing the recv() call or decreasing the poll/select interval but that also can increase load.
Detecting this is generally as you have done, being in SCF. Dealing with them is generally outside of operational control - and there are tradeoffs in the application to make this as seamless as possible. This happens a lot with web servers.
Good luck.