title: Creating a TCP Socket connection and keep the connection open across several views
body: <p>I have successfully made a connection from an iPhone to a Server (which is a Windows machine) using TCP sockets.  Currently, I'm using a button to execute the following code:</p>
  
  <pre><code>while(1)
  {
      Socket *socket;
      int port = 11005;
      NSString *host = @"9.5.3.63";
  
      socket = [Socket socket];
  
      @try
      {
          NSMutableData *data;
          [socket connectToHostName:host port:port];
          [socket readData:data];
          //  [socket writeString:@"Hello World!"];
  
          //** Connection was successful **//
          [socket retain]; // Must retain if want to use out of this action block.
      }
      @catch (NSException* exception) 
      {
          NSString *errMsg = [NSString stringWithFormat:@"%@",[exception reason]];
          NSLog(errMsg);
          socket = nil;
      }
  }
  </code></pre>
  
  <p>That was the easy part... I'm trying to establish the sockets connection as soon as the app loads.  I tried putting this code in my viewDidLoad, but the loop is infinite and the view never loads.  I have several views in my project, and I'd like to open the connection keep the connection open at all times, across all views.</p>
  
  <p>Objective:</p>
  
  <ul>
  <li>Open TCP Sockets connection when app first loads</li>
  <li>Maintain connection infinitely, no matter what view I am in (Multiple Views in Project)</li>
  </ul>
  
  <p>I'm still rather new to iOS development, so I appreciate as much clarity as possible.  It should be noted that I am using the SmallSockets library to open my Sockets connection.  Thanks for the help!</p>
  
  <p><strong>* EDIT *</strong></p>
  
  <p>Based off the answer below, this is what I've got going so far:</p>
  
  <p>SocketConnection.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface SocketConnection : NSObject
  {
  
  }
  
  + (SocketConnection *)getInstance;
  
  @end
  </code></pre>
  
  <p>SocketConnection.m</p>
  
  <p>static SocketConnection *sharedInstance = nil;</p>
  
  <pre><code>@implementation SocketConnection
  
  - (id)init
  {
      self = [super init];
  
      if (self) 
      {
          while(1)
          {
              Socket *socket;
              int port = 11005;
              NSString *host = @"9.5.3.63";
  
              socket = [Socket socket];
  
              @try
              {
                  NSMutableData *data;
                  [socket connectToHostName:host port:port];
                  [socket readData:data];
                  //  [socket writeString:@"Hello World!"];
  
                  //** Connection was successful **//
                  [socket retain]; // Must retain if want to use out of this action block.
              }
              @catch (NSException* exception) 
              {
                  NSString *errMsg = [NSString stringWithFormat:@"%@",[exception reason]];
                  NSLog(errMsg);
                  socket = nil;
              }
          }
      }
      return self;
  }
  
  + (SocketConnection *)getInstance
  {
      @synchronized(self) 
      {
          if (sharedInstance == nil) 
          {
              sharedInstance = [[SocketConnection alloc] init];
          }
      }
      return sharedInstance;
  }
  
  @end
  </code></pre>
  
  <p>I still have not figured out how the singleton class gets invoked.  I fired up my app with the code from above and it's not connecting to the server.  Any ideas?</p>
  
  <p>Thanks!</p>