tcp connections are expensive (you are paying the price)
looks to me like you have a backlog of "pings". (there is a limit
to the number of connections that can be established). when
you open a socket, eventually there is a corresponding close.
at some point, the close_wait is satisfied, the port is closed
and made available for connections.
in addition, it looks to me like you're opening a new connection to
carry out your "pings".
it's not completely clear why you have to ping, but
let's assume it's to make sure the current work connection
is alive. when you ping from a different port, even if it's to the
same server on its listening port, you are essentially simply
verifying that the server is alive, not that the connection
the work is being done on is alive. the connection you're doing
the work on could well be dead; the ping is coming
from a different port. the only way to know if the
current connection is still alive is to ping from it.
without opening a new connection to carry out the ping
(if that, in fact, is what's occurring. which is what i think is.)
if the ping doesn't answer, then (and only then) would you
want to open a new work connection. fyi, websockets has
this kind of mechanism already built in.
close_wait is part of a connection shutdown. if the other side
does not acknowledge any part of that shutdown, close_wait
waits. if a server is configured to handle X number of connections,
and they're all tied up waiting for wait_close from your pings
to clear, the new connections are dropped.