title: Delphi XE2 / Indy TIdTCPServer / "Connection reset by peer"
body: <p>I'm with one problem using Indy in Delphi XE2 to send TCP Messages using TIdTCPServer.</p>
  
  <p>For exemple:
  I have 2 devices and i'll go communicate with device 1.
  When i send messages to device 1, the messages were send fine.
  But without close the program, when i send messages to device 2, Delphi returns "Connection reset by peer".</p>
  
  <p>Below is my code:</p>
  
  <pre><code>procedure TMainHost.idTCPServerNewConnect(AContext: TIdContext);
  var
    Client: TSimpleClient;
  begin
    Sleep(1000);
    Client := TSimpleClient.Create();
  
    Client.DNS := AContext.Connection.Socket.Host;
    Client.Conectado := True;
    Client.Port := idTCPServerNew.DefaultPort;
    Client.Name := 'Central';
    Client.ListLink := Clients.Count;
    Client.Thread := AContext;
    Client.IP := AContext.Connection.Socket.Binding.PeerIP;
  
    AContext.Data := Client;
  
    Clients.Add(Client);
    Sleep(500);
  
    if (MainEstrutura.current_central.IP = Client.IP) then
    begin
      MainEstrutura.current_central.Conectado := true;
      MainEstrutura.envia_configuracao;
    end;
  
  end;
  
  procedure TMainHost.idTCPServerNewDisconnect(AContext: TIdContext);
  var
    Client: TSimpleClient;
  begin
    { Retrieve Client Record from Data pointer }
    Client := Pointer(AContext.Data);
    { Remove Client from the Clients TList }
    Clients.Remove(Client);
    { Free the Client object }
    FreeAndNil(Client);
    AContext.Data := nil;
  
  end;
  </code></pre>
  
  <p>To send the messages to devices:</p>
  
  <pre><code>procedure TMainHost.DirectTCPMessage(IP: String; TheMessage: String);
  var
    Client: TSimpleClient;
    i: Integer;
    List: TList;
    Msg: String;
  begin
  
    Msg := Trim(TheMessage);
  
    for i := 0 to Clients.Count - 1 do
    begin
  
      Client := TSimpleClient(Clients.Items[i]);
  
      if TIdContext(Client.Thread).Connection.Socket.Binding.PeerIP = IP then
      begin
  
        TIdContext(Client.Thread).Connection.Socket.WriteLn(Msg);
  
      end;
  
    end;
  end;
  </code></pre>
  
  <p>And i have another problem.</p>
  
  <p>When i set active := False on tidtcpserver Component, the application crashes.
  Thanks!</p>