title: How to Maintain VOIP socket connection in background?
body: <p><strong>My App Requirement</strong>: I should maintain a socket connection to trigger local notification on server push without using Push Notification(APNs) for some reasons. So I am using the VOIP background capability of iPhone to maintain socket connection.</p>
  
  <p><strong>1. I have configured a stream for VOIP in order to persist socket connection to run in background, so what Timeout value should I set?
  Will the socket connection terminates once the timeout expires?
  How do I make my application to listen to the socket all the time.?</strong></p>
  
  <p>Client stream configuration is as follows,</p>
  
  <pre><code>NSString *urlStr = @"http://192.168.0.108";
  NSURL *website = [NSURL URLWithString:urlStr];
  CFReadStreamRef readStream;
  CFWriteStreamRef writeStream;
  CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &amp;readStream, &amp;writeStream);
  
  CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
  CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);    
  
  NSInputStream *inputStream = (NSInputStream *)readStream;
  NSOutputStream *outputStream = (NSOutputStream *)writeStream;
  [inputStream setDelegate:self];
  [inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
  [outputStream setDelegate:self];
  [outputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
  [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  [inputStream open];
  [outputStream open];
  </code></pre>
  
  <p><strong>2. Should I reconnect the stream in the handler applicationDidEnterBackground:</strong></p>
  
  <pre><code>   [[UIApplication sharedApplication] setKeepAliveTimeout:86400 handler:^(void) 
  {
  
      if (inputStream)
          [inputStream close];
      if (outputStream)
          [outputStream close];
  
  
      urlStr = @"http://192.168.0.108";
      website = [NSURL URLWithString:urlStr];
      CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &amp;readStream, &amp;writeStream);
      CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
      CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);    
      inputStream = (NSInputStream *)readStream;
      outputStream = (NSOutputStream *)writeStream;
      [inputStream setDelegate:self];
      [inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
      [outputStream setDelegate:self];
      [outputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
      [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
      [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
      [inputStream open];
      [outputStream open];
  
      }];
  </code></pre>
  
  <p><strong>3. Say my server restarts and the app is in background, how do I ensure the connection?</strong>
       If the Wi-Fi connection in my iPhone or if I terminate the server app the connection will be closed, so what measures should I take to make my app work as per expectation?</p>