TWinSocketStream.Read(): Read error 6, The handle is invalid

95% of following code was part of a answer gave by user @Remy Lebeau. I'm making this question only to that he clarify 1 small doubt that i asked in last comment (about his code) and not was possible he answered because moderator deleted my account (and also my previous question).

The doubt was about the error described on title of this question:

  • This exception occurs when i'm closing the TServerSocket connection (Active := False).

On code below the line of error is ( procedure readRaw() ):

Received := SocketStrm.Read(P^, Size);

Then how prevent this exception?

@Remy said that i must ignore but how?

try, except, break for example?

I tried this possibility but without success.

    type
      TCMDSock_Thread = class(TServerClientThread)
      private
        Manufacturer, Model, OsVersion, SIMOpName, SIMNumber: string;
        procedure addClientToListView;
        procedure removeClientFromListView;
      protected
        procedure ClientExecute; override;
      end;

    var
      FMain: TFMain;

    implementation

    {$R *.dfm}

    //================================================================================================================

    procedure TCMDSock_Thread.ClientExecute;
    var
      SocketStrm: TWinSocketStream;
      ReceivedText: string;

      procedure readRaw(var Data; Size: Integer);
      var
        P: PByte;
        Received: Integer;
      begin
        P := PByte(@Data);
        while Size > 0 do
        begin
          Received := SocketStrm.Read(P^, Size);
          if Received <= 0 then SysUtils.Abort;
          Inc(P, Received);
          Dec(Size, Received);
        end;
      end;

      procedure writeRaw(const Data; Size: Integer);
      var
        P: PByte;
        Sent: Integer;
      begin
        P := PByte(@Data);
        while Size > 0 do
        begin
          Sent := SocketStrm.Write(P^, Size);
          if Sent <= 0 then SysUtils.Abort;
          Inc(P, Sent);
          Dec(Size, Sent);
        end;
      end;

      function readMessage: boolean;
      var
        Tmp: UTF8String;
        Len: Integer;
      begin
        Result := SocketStrm.WaitForData(100);
        if not Result then Exit;

        readRaw(Len, sizeof(Len));
        Len := ntohl(Len);

        SetLength(Tmp, Len);
        readRaw(PAnsiChar(Tmp)^, Len);

        ReceivedText := string(Tmp);
      end;

      procedure writeString(const S: string);
      var
        Tmp: UTF8String;
      begin
        Tmp := UTF8String(S);
        writeRaw(PAnsiChar(Tmp)^, Length(Tmp));
      end;

      procedure writeLine(const S: string);
      begin
        writeString(S + #13#10);
      end;

      function splitData: boolean;
      var
        Idx: Integer;
      begin
        Result := StartsText('<|data|>', ReceivedText);
        if not Result then Exit;

        Delete(ReceivedText, 1, 8);
        Idx := Pos('<|>', ReceivedText);
        Manufacturer := Copy(ReceivedText, 1, Idx-1);

        Delete(ReceivedText, 1, Idx+2);
        Idx := Pos('<|>', ReceivedText);
        Model := Copy(ReceivedText, 1, Idx-1);

        Delete(ReceivedText, 1, Idx+2);
        Idx := Pos('<|>', ReceivedText);
        OsVersion := Copy(ReceivedText, 1, Idx-1);

        Delete(ReceivedText, 1, Idx+2);
        Idx := Pos('<|>', ReceivedText);
        SIMOpName := Copy(ReceivedText, 1, Idx-1);

        Delete(ReceivedText, 1, Idx+2);
        Idx := Pos('<<|', ReceivedText);
        SIMNumber := Copy(ReceivedText, 1, Idx-1);

      end;

    begin
      try
        SocketStrm := TWinSocketStream.Create(ClientSocket, 5000);
        try
          writeLine('info');
          try
            while (not Terminated) and ClientSocket.Connected do
            begin
              if readMessage then
              begin
                if splitData then
                  Synchronize(addClientToListView);
              end;
            end;
          finally
            writeLine('disconnect');
          end;
        finally
          SocketStrm.Free;
        end;
      finally
        Synchronize(removeClientFromListView);
      end;
    end;

    procedure TCMDSock_Thread.addClientToListView;
    var
      Item: TListItem;
    begin
      Item := FMain.ListView1.FindData(0, ClientSocket, True, False);
      if Item = nil then
      begin
        Item := FMain.ListView1.Items.Add;
        Item.Data := ClientSocket;
      end;
      Item.Caption := IntToStr(ClientSocket.Handle);
      Item.SubItems.Add(ClientSocket.RemoteAddress);
      Item.SubItems.Add(ClientSocket.RemoteHost);
      Item.SubItems.Add(Manufacturer +' - '+Model+' - '+OsVersion);
      Item.SubItems.Add(SIMOpName+' - '+SIMNumber);
    end;

    procedure TCMDSock_Thread.removeClientFromListView;
    var
      Item: TListItem;
    begin
      Item := FMain.ListView1.FindData(0, ClientSocket, True, False);
      if Item <> nil then
        Item.Delete;
    end;

    procedure TFMain.ServerSocket1GetThread(Sender: TObject; ClientSocket: TServerClientWinSocket; var SocketThread: TServerClientThread);
    begin
      SocketThread := TCMDSock_Thread.Create(False, ClientSocket);
    end;


    /////////////////////////////////////////////// HERE IS MY TROUBLE WITH THE EXCEPTION /////////////////////////////////////////////////////////
    procedure TFMain.Button1Click(Sender: TObject); // <= PopupMenu
    begin
      ServerSocket1.Active := False;
    end;

PS: Moderator, i'm want only this answer, then after it you can delete my account by if want ;-)