title: How to call Socket Send and Receive multiple times without closing the socket?
body: <p>I'm using the .NET <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx" rel="nofollow">Socket</a> class.</p>
  
  <p>Basically my program is to send XML commands via TCP to my server (running some 3rd party services), and it will send back an XML response.</p>
  
  <p>What I wanted to do is to send and receive multiple times without closing the socket, because the server seems to slow down if I open and close the socket too many times in a short period of time.</p>
  
  <p>Stripped down version of my code:</p>
  
  <pre><code>//Connects to server
  s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  IPEndPoint ipe = new IPEndPoint("123.123.123.123", 8199);
  socket.Connect(ipe);
  
  //Send my message
  int sent = sock.Send(buffer, buffer.Length, SocketFlags.None); //buffer contains my XML data
  
  //Receives respond from server
  byte[] buf = new byte[65536];
  int ret = socket.Receive(buffer, 0, 65536, SocketFlags.None);
  
  //Send my message again without closing the socket
  sent = sock.Send(buffer, buffer.Length, SocketFlags.None); //buffer contains my XML data
  
  //Receives respond from server failed..
  ret = socket.Receive(buffer, 0, 65536, SocketFlags.None);
  </code></pre>
  
  <p>It send/receive works the first time, but not the second time. The second time it throws an exception "An existing connection was forcibly closed by the remote host". What do I need to do before I can issue another send/receive?</p>
  
  <p>Thank you so much for reading.</p>