title: How to test if my remote socket NSStream are correctly open
body: <blockquote>
    <p>TL;DR : What's the way to check if my remote stream are opened correctly after a call to <code>NSStream.getStreamsToHostWithName(...)</code>?</p>
  </blockquote>
  
  <p>My application is a mobile IOS8 swift application.</p>
  
  <p>I am using NSStream for input and output socket communication with a remote server. </p>
  
  <p>To connect to my server and open my stream I use this code:</p>
  
  <pre><code>func connect(host: String, port: Int) -&gt; Bool
  {
      //clear the previous connection if existing (and update self.connected)
      disconnect()
      //updating the current connection
      self.host = host
      self.port = port
  
      //pairing NSstreams with remote connection
      NSStream.getStreamsToHostWithName(self.host!, port: self.port!, inputStream: &amp;inputStream, outputStream: &amp;outputStream)
  
      if (self.inputStream != nil &amp;&amp; self.outputStream != nil)
      {
          //open streams
          self.inputStream?.open()
          self.outputStream?.open()
      }
      if self.outputStream?.streamError == nil &amp;&amp; self.inputStream?.streamError == nil
      {
          println("SOK")    //PROBLEM 1
      }
      //error checking after opening streams // PROBLEM 2
      if var inputStreamErr: CFError = CFReadStreamCopyError(self.inputStream)?
      {
          println("InputStream error : " + CFErrorCopyDescription(inputStreamErr))
      }
      else if var outputStreamErr: CFError = CFWriteStreamCopyError(self.outputStream)?
      {
          println("OutStream error : " + CFErrorCopyDescription(outputStreamErr))
      }
      else
      {
          //set the delegate to self
          self.inputStream?.delegate = self
          self.outputStream?.delegate = self
          self.connected = true
      }
      //return connection state
      return self.connected
  }
  </code></pre>
  
  <p>My problem is located at //PROBLEM1 and //PROBLEM2.</p>
  
  <p>At these points I try to determine if my sockets are opened correctly, but even if the server is not running this code still works, then the read and write operations are failing.
  I would like to be able to determine if the connection failed or not.</p>
  
  <p>Maybe I am doing it totally wrong, I don't get how to test this.</p>