I think you need to add code to handle the Disconnect event. I had a similar problem with what you described, and here is what I did (in this example, tcpServer is an instance of TIdTCPServer):
procedure TformRemoteControlWnd.tcpServerDisconnect(AContext: TIdContext); (* Connection is disconnected. Be careful, because this also gets called when the app is shutting down while a connection is active, in which case tcpServer may be gone already. *) begin if not Application.Terminated and Assigned(tcpServer) then begin sbarStatus.SimpleText := 'TCP/IP Disconnected'; tcpServer.Tag := 0; // used to prevent rentrancy end; // shut down connection to stop thread from calling OnExecute event try AContext.Connection.Disconnect; except Sleep(0); end; end;
source share