title: Issue in writing `NSOutputStream`.
body: <p>I have one basic question, 
  While working with <code>NSOutputStream</code>, should we wait for <code>NSStreamEventHasSpaceAvailable</code> to send the packet, so we can call , <code>[NSOutputStream write]</code> as and when its needed, </p>
  
  <p>I believe <code>NSStream</code> should take care of write function... </p>
  
  <p>if this is not correct, then please provide your views on following logic, </p>
  
  <p>===== To Write on <code>NSOutputStream</code> =================
  Have Queue to add packet, that to be sent 
      // <code>StreamQueue.h</code>  </p>
  
  <pre><code>@interface StreamQueue : NSObject &lt;NSCoding&gt;
  {
      NSMutableArray * data;
      NSRecursiveLock * theLock;
  }
  
  #pragma mark �Initialization &amp; Deallocation�
  - (id)init;
  - (id)initWithQueue:(CommQueue *)queue;
  - (id)initWithCoder:(NSCoder *)coder;
  - (void)dealloc;
  - (void)encodeWithCoder:(NSCoder *)coder;
  
  #pragma mark
  #pragma mark �Accessor Methods�
  - (int)size;
  - (BOOL)isEmpty;
  - (id)top;
  - (NSArray *)data;
  
  #pragma mark
  #pragma mark �Modifier Methods�
  - (void)enqueue:(id)object;
  - (id)dequeue;
  - (void)removeAll;
  @end
  </code></pre>
  
  <p>and its implementation </p>
  
  <pre><code>#import "StreamQueue.h"
  
  
  @implementation StreamQueue
  #pragma mark �Initialization &amp; Deallocation�
  - (id)init
  {
      if (self = [super init]) {
          data = [[NSMutableArray alloc] init];
          theLock = [[NSRecursiveLock alloc] init];
      }
      return self;
  }
  
  - (id)initWithQueue:(StreamQueue *)queue
  {
      if (self = [super init]) {
          data = [[NSMutableArray alloc] initWithArray:[queue data]];
          theLock = [[NSRecursiveLock alloc] init];
      }
      return self;
  }
  
  - (id)initWithCoder:(NSCoder *)coder
  {
      if (self = [super init]) {
          data = [[NSMutableArray alloc] initWithArray:[coder decodeObject]];
          theLock = [[NSRecursiveLock alloc] init];
      }
      return self;
  }
  
  - (void)dealloc
  {
      [data release];
      [theLock release];
      [super dealloc];
  }
  
  - (void)encodeWithCoder:(NSCoder *)coder;
  {
      [coder encodeObject:data];
  }
  
  #pragma mark
  #pragma mark �Accessor Methods�
  - (int)size
  {
      int size;
      [theLock lock];
      size = [data count];
      [theLock unlock];
      return size;
  }
  
  - (BOOL)isEmpty
  {
      BOOL empty;
      [theLock lock];
      empty = ([data count] == 0);
      [theLock unlock];
      return empty;
  }
  
  - (id)top
  {
      id object = nil;
      [theLock lock];
      if (![self isEmpty])
          object = [data objectAtIndex:0];
      [theLock unlock];
      return object;
  }
  
  - (NSArray *)data
  {
      NSArray * array;
      [theLock lock];
      array = [NSArray arrayWithArray:data];
      [theLock unlock];
      return array;
  }
  
  #pragma mark
  #pragma mark �Modifier Methods�
  - (void)enqueue:(id)object
  {
      [theLock lock];
      [data addObject:object];
      [theLock unlock];
  }
  
  - (id)dequeue
  {
      id object = [self top];
      if (object != nil) {
          [theLock lock];
          [object retain];
          [data removeObjectAtIndex:0];
          [theLock unlock];
      }
      return [object autorelease];
  }
  
  - (void)removeAll
  {
      [theLock lock];
      while (![self isEmpty])
          [data removeObjectAtIndex:0];
      [theLock unlock];
  }
  @end
  </code></pre>
  
  <p>Now when Application have something to send over socket(<code>NSStream</code>), it should add it into the queue, </p>
  
  <pre><code>-(bool)sendRawData:(const uint8_t *)data length:(int)len{
  
      // if still negotiating then don't send data
      assert(!networkConnected);
  
      NSData *pData  = [NSData dataWithBytes:(const void *)data length:len];
  
      // pToSendPacket is of type StreamQueue 
      [pToSendPacket enqueue:pData];
  
      return;
  }
  </code></pre>
  
  <p>and this piece of code when we get <code>NSHasSpaceAvailableEvent</code> </p>
  
  <pre><code>-(void)gotSpaceAvailable{
      // is there any pending packets that to be send. 
      NSData *pData = (NSData *)[pToSendPacket dequeue];
  
      if(pData == nil){
          // no pending packets.. 
          return;
      }
  
      const uint8_t *data = (const uint8_t *)[pData bytes];
      int len = [pData length];
  
      int sendlength = [pOutputStream write:data maxLength:len];
  
      if(sendlength == -1 ){
          NSError *theError = [pOutputStream streamError];
          NSString *pString = [theError localizedDescription];
          int errorCode = [theError code];
          return ;
      }
  }
  </code></pre>
  
  <p>I was expecting Application will keep on receiving the event, whenever <code>OutputStream</code> sends data, but i recieved only once... :( 
  Please help ... </p>