// Keep a reference to self to use for controller callbacks//CFStreamClientContextctx={0,(__bridgevoid*)(self),NULL,NULL,NULL};// Get callbacks for stream data, stream end, and any errors//CFOptionFlagsregisteredEvents=(kCFStreamEventHasBytesAvailable|kCFStreamEventEndEncountered|kCFStreamEventErrorOccurred);// Create a read-only socket//CFReadStreamRefreadStream;CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,(__bridgeCFStringRef)host,(UInt32)port,&readStream,NULL);// Schedule the stream on the run loop to enable callbacks//if(CFReadStreamSetClient(readStream,registeredEvents,socketCallback,&ctx)){CFReadStreamScheduleWithRunLoop(readStream,CFRunLoopGetCurrent(),kCFRunLoopCommonModes);}else{[selfnetworkFailedWithErrorMessage:@"Failed to assign callback method"];return;}// Open the stream for reading//if(CFReadStreamOpen(readStream)==NO){[selfnetworkFailedWithErrorMessage:@"Failed to open read stream"];return;}CFErrorReferror=CFReadStreamCopyError(readStream);if(error!=NULL){if(CFErrorGetCode(error)!=0){NSString*errorInfo=[NSStringstringWithFormat:@"Failed to connect stream; error '%@' (code %ld)",(__bridgeNSString*)CFErrorGetDomain(error),CFErrorGetCode(error)];[selfnetworkFailedWithErrorMessage:errorInfo];}CFRelease(error);return;}