title: GCDAsyncUdpSocket, "Cannot bind socket more than once"
body: <p>I'm using GCDAsyncUdpSocket to get udp broadcast between iphone and remote udp server.
  I'm sending a small "hello" on a specif port to "255.255.255.255" broadcast address.</p>
  
  <p>Then the  server replies, allowing me to discover its ip address.</p>
  
  <p>Everything works fine, especially using the simulator, except that if I run it once on the iphone,
  when i try to stop the app and run it right away after, I get a "Cannot bind socket more than once" error. This happens when I click stop in xcode or when I kill the app in IOS.</p>
  
  <p>Here is a sample of my code :</p>
  
  <pre><code>#import "GCDAsyncUdpSocket.h"
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      if (udpSocket == nil)
      {
          [self setupSocket];
      }
  }
  
  - (void)setupSocket
  {
  
      udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
      [udpSocket enableBroadcast:YES error:nil];
  
  
      NSData *data = [@"hello" dataUsingEncoding:NSUTF8StringEncoding];
      [udpSocket sendData:data toHost:@"255.255.255.255" port:21180 withTimeout:-1 tag:tag++];
  
      NSError *error = nil;
  
      if (![udpSocket bindToPort:0 error:&amp;error])
      {
          [self logError:FORMAT(@"Error binding: %@", error)];
          return;
      }
      if (![udpSocket beginReceiving:&amp;error])
      {
          [self logError:FORMAT(@"Error receiving: %@", error)];
          return;
      }
  }
  
  - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
        fromAddress:(NSData *)address
  withFilterContext:(id)filterContext
  {
      NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      if (msg)
      {
          [self logMessage:FORMAT(@"RECV: %@", msg)];
      }
      else
      {
          NSString *host = nil;
          uint16_t port = 0;
          [GCDAsyncUdpSocket getHost:&amp;host port:&amp;port fromAddress:address];
  
          [self logInfo:FORMAT(@"RECV: Unknown message from: %@:%hu", host, port)];
      }
      [udpSocket close];
      udpSocket = NIL;
  
  }
  </code></pre>
  
  <p>In fact it seems that socket reference is still binded, ignoring close action and setting to nil.</p>
  
  <p>Any advice is welcome,
  Thanks for reading.</p>
  
  <p>FKY</p>