I guess that it only occurs when there are multiple retries. Then your
TcpClient
might be no longer valid due to the
Close()
call:
The Close method marks the instance as disposed and requests that the associated Socket close the TCP connection.
Note the "disposed".
There is no need to perform the
Connected
check at all because it would be always
false
in your code after closing. But if the
Close()
call makes the object invalid, you might then get the same error at the next
Connect()
call. The only solution would be then using a new instance (optionally limited to the case that the
Connect()
call does not throw an exception; when that throws an exception, there is no need to close).