title: Delphi - How Indy TCPServer manage clients connections?
body: <p>I am creating a client-server application to run only on Windows OS, and each Windows user will connect to server at a time. The LAN uses DHCP, so IP is not fixed.</p>
  
  <p>I need to send a string message from IdTCPServer to a specific IdTCPClient. In the beginning I was using a Listbox, so I added the hostname to the listbox when connected and remove when disconnect. At that time, Remy Lebeau give me this tip:</p>
  
  <pre><code>procedure TfrmMain.sendButtonClick(Sender: TObject);
  var
    Index: Integer;
    Ctx: TIdContext;
  begin
    Index := ListBox.ItemIndex;
    if Index = -1 then Exit;
    Context := TIdContext(ListBox.Items.Objects[Index]);
    // use Context as needed...
  end;
  </code></pre>
  
  <p>But now I am using a ListView, with pre-added hostnames. So I just change the listbox item image status when clients connect or disconnect. Now I am trying something like this:</p>
  
  <pre><code>procedure TfrmMain.TCPServerConnect(AContext: TIdContext);
  begin
    TThread.Queue(nil,
      procedure
      var
        Host: String;
        LItem: TListItem;
      begin
        Host := UpperCase(GStack.HostByAddress(Ctxt.Binding.PeerIP));
        LItem := lvwPCList.FindCaption(0, Host, False, True, False);
        if (LItem &lt;&gt; nil) then LItem.Data := AContext.Data;
      end
    );
  end;
  </code></pre>
  
  <p>And once I linked the Listview Item with the Context data, I am trying to send the message direct to client:</p>
  
  <pre><code>procedure TfrmMain.SendMessage(const Item: TListItem; const Msg: String);
  var
    Ctx: TIdContext;
  begin
    if (Trim(Msg) = '') then Exit;
    Ctx := TIdContext(Item.Data);
    try
      Ctx.Connection.IOHandler.WriteLn(Msg);
    except
    end;
  end;
  </code></pre>
  
  <p>I can compile this code, but the message never reachs client. Please, what I am doing wrong?</p>
  
  <p>Thanks!</p>