Socket.Active not made false after closing socket

Hi all,

In a project, I am using TClientSocket object in my form to connect to
a remote listening socket. (The scope of the project is limited to the
client implementation , and not the server. And I have no visibility
of the server software).

As per a particular requirement, I need to forcibly close the client
socket. This is triggering the DisconnectEvent and the corresponding
event handler is getting called. But in the Disconnect event handler,
the socket is again closed only if socket.connected is true. The code
segment is as follows:

within a custom routine (where I am forcibly closing the client
socket)
-----------------------------------------------------------------------
TestFrm.TestClient.socket.close;

within Disconnect event handler
-------------------------------
    if(Testfrm.TestClient.socket.connected)then
      TestFrm.TestClient.socket.close;

But this always causes an exception 10038 (WSAENOTSOCK: An operation
was attempted on something that is not a socket), which means I am
trying to close a socket which is already closed. I am sure, this is
happening within the Disconnect event handler. i.e. socket.connected
is not false even after closing the socket from the custom routine.

I tried modifying the disconnect event handler as follows

    if(Testfrm.TestClient.Active)then
      TestFrm.TestClient.socket.close;

and this gave no error as Active becomes false on socket.close. But,
whether (Testfrm.TestClient.Active) serves the same purpose as
(Testfrm.TestClient.socket.connected) at least in this requirement.

Please note that I am including certain additional requirements in an
already existing software and I must not change the already developed
portions. Unfortunately the Disconnect event handler is developed by
someone else long back.

Thankyou.

Shehan