title: Check if socket is connected in compact framework
body: <p>I have a server software running on windows desktop and developed in .net that is always listening for incoming connection on sockets through TCP/IP.</p>
  
  <p>Then I have a client software running on CE 6.0 developed with compact framework 3.5...This client need to:</p>
  
  <ul>
  <li>Connect to the server when service is enabled</li>
  <li>Send few bytes of data every 300 ms</li>
  <li>Never hang while sending or lose time connecting every time</li>
  <li>Check if socket is connected</li>
  </ul>
  
  <p>So far I implemented the server side with an Async code based on this sample but modified to keep connections alive until server close them...not only for one shot and it's perfectly working</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/fx6588te.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/fx6588te.aspx</a></p>
  
  <p>then I implemented the client side this way:</p>
  
  <pre><code>dim clientSocket as new System.Net.Sockets.TcpClient()
  
  /... code .../
  
  'Activate the service
  clientSocket.Connect("10.0.0.34", myPort)
  
  / .... code ..../ 
  
  'Data send function
  dim serverStream as NetworkStream = clientSocket.GetStream()
  dim outStream as byte() = System.Text.Enconding.ASCII.GetBytes("My messagge...")
  serverStream.Write(outStream, 0, outStream.Length)
  serverStream.Flush
  </code></pre>
  
  <p>Also this is perfectly working. What do I need?</p>
  
  <ol>
  <li>To detect if connection with socket is open before send data...this will avoid loss of time in case something went wrong</li>
  </ol>
  
  <p>I searched among the web but I couldn't do a way to do it in CF...and the .connected method is not available here.</p>
  
  <p>The best - but it seems stupid to me - way I found is to set a flag when connection/send is successful and to reset it when something goes wrong.</p>