title: TCP connections with NSStream/CFStream
body: <p>I am desperately trying to figure out how to detect errors when opening TCP streams using <code>NSStream +getStreamsToHost</code>/<code>CFStreamCreatePairWithSocket()</code>. If I do this:</p>
  
  <pre><code>NSInputStream* input = nil;
  NSOutputStream* output = nil;
  [NSStream getStreamstoHost:[NSHost hostWithName:@"localhost"] port:80 inputStream:&amp;input outputStream:&amp;output];
  
  NSError* error1 = [input streamError];
  assert(error1 == nil);
  NSStreamStatus status1 = [input streamStatus];
  
  [input open];
  NSError* error2 = [input streamError];
  assert(error2 == nil);
  NSStreamStatus status2 = [input streamStatus];
  </code></pre>
  
  <p><code>status1</code> is <code>NSStreamStatusNotOpen</code>, which is expected. <code>error1</code> is <code>nil</code>. <code>error2</code> is also <code>nil</code>, and <code>status2</code> is <code>NSStreamStatusOpening</code>. If I telnet to the same address, I get connection refused - there is nothing listening on port 80. If I try to connect to some nonsensical address, such as <code>yaddayadda</code>, I get <code>nil</code> streams.</p>
  
  <p>How do I handle errors properly? No example anywhere seems to handle error conditions, and the docs don't say anything about it, other than the streams may be nil. I'm stumped. Don't tell me I have to run the connection through a run loop, just to get proper error handling...?</p>
  
  <p>I know there's always the possibility of using good ol' BSD sockets, but the docs warns against that, as some high level networking features may fail (auto connections over VPN, and similar stuff).</p>