void UDPServer(){
   int sockfd;
   int device_type = 1;
   struct sockaddr_in servaddr,cliaddr;
   socklen_t len;
   char buf[512];
   char *up = "U";
   char *down = "D";
   char *left = "L";
   char *right = "R";
   char *fire = "F";
   char *stop = "S";
   char msg = 0x00;

   sockfd = socket(AF_INET, SOCK_DGRAM, 0);

   bzero(&servaddr, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
   servaddr.sin_port = htons(20000);
   bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
   
   fprintf(stderr, "Waiting for connections.... (%s:%d)\n", inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port));
   
   for (;;)
   {
      len = sizeof(cliaddr);
      recvfrom(sockfd,buf,512,0,(struct sockaddr *)&cliaddr, &len);

    fprintf(stderr, "Received command %s from %s\n", buf, inet_ntoa(cliaddr.sin_addr));         
    
    if (strncmp(buf,left,1) == 0)
      msg = MISSILE_LAUNCHER_CMD_LEFT;
    
    else if (strncmp(buf,right,1) == 0)
      msg = MISSILE_LAUNCHER_CMD_RIGHT;
    
    else if (strncmp(buf,up,1) == 0)
      msg = MISSILE_LAUNCHER_CMD_UP;
    
    else if (strncmp(buf,down,1) == 0)
      msg = MISSILE_LAUNCHER_CMD_DOWN;
    
    else if (strncmp(buf,fire,1) == 0)
      msg = MISSILE_LAUNCHER_CMD_FIRE;
      
    else if (strncmp(buf,stop,1) == 0)
      msg = MISSILE_LAUNCHER_CMD_STOP;

    
    missile_do(control, msg, device_type);
    usleep(300 * 1000);
    missile_do(control, MISSILE_LAUNCHER_CMD_STOP, device_type);
    msg = 0x00;
   }

  close(sockfd);
}
int main(int argc, char **argv) {
  const char *optstring = "hd:c:t:FLRUDS:";

  int o;
  int delay = 0;

  int device_type = 1;
  
  char *device = NULL;

  unsigned int set_event = 0;
  unsigned int set_fire = 0, set_left = 0, set_right = 0;
  unsigned int set_up = 0, set_down = 0, set_stop = 0;

  //---------------------------------------------------------------------------
  // Get args

  o = getopt(argc, argv, optstring);

  while (o != -1) {
    switch (o) {

    case 'h':
      usage(argv[0]);
      return 0;

    case 'd':
      debug_level = atoi(optarg);
      fprintf(stderr, "debug_level set to %d\n", debug_level);
      break;

    case 'c':
      device = optarg;
      set_event = 1;
      break;

    case 't':
      device_type = atoi(optarg);
      break;

    case 'F':
      set_fire = 1;
      break;

    case 'U':
      set_up = 1;
      break;

    case 'D':
      set_down = 1;
      break;

    case 'L':
      set_left = 1;
      break;

    case 'R':
      set_right = 1;
      break;

    case 'S':
      set_stop = 1;
      delay = atoi(optarg);
      break;

    }
    o = getopt(argc, argv, optstring);
  }

  //---------------------------------------------------------------------------

  if (missile_usb_initialise() != 0) {
    fprintf(stderr, "missile_usb_initalise failed: %s\n", strerror(errno));
    return -1;
  }
  
  control = missile_usb_create(debug_level, USB_TIMEOUT);
  if (control == NULL) {
    fprintf(stderr, "missile_usb_create() failed\n");
    return -1;
  }
  
  if (missile_usb_finddevice(control, 0, device_type) != 0) {
    fprintf(stderr, "USBMissileLauncher device not found\n");
    return -1;
  }

  if (debug_level)
    fprintf(stderr, "Now we're ready.  Move the thing around, and FIRE!\n");

  //---------------------------------------------------------------------------

  int fd;
  struct input_event ev[64];
  int events;

  if (set_event) {

    if (device == NULL) {
      fprintf(stderr, "No device given\n");
      usage(argv[0]);    
      return 0;
    }
    
    if (IEOpen(device, &fd) < 0) {
      fprintf(stderr, "IEOpen(%s, fd) failed\n", device);
      return -1;
    }

    while (1) {

      if (IERead(fd, ev, &events) < 0) {
	fprintf(stderr, "IERead() failed\n");
	return -1;
      }

      if (HandleKeyboardEvent(ev, events, device_type) < 0) {
	fprintf(stderr, "HandleKeyboardEvent() failed\n");
	return -1;
      }

      usleep(200000);
	
    }
    
  }

  //---------------------------------------------------------------------------

  char msg = 0x00;

  switch (device_type) {
    
  case DEVICE_TYPE_MISSILE_LAUNCHER:
  
    if (set_left)
      msg |= MISSILE_LAUNCHER_CMD_LEFT;
    
    if (set_right)
      msg |= MISSILE_LAUNCHER_CMD_RIGHT;
    
    if (set_up)
      msg |= MISSILE_LAUNCHER_CMD_UP;
    
    if (set_down)
      msg |= MISSILE_LAUNCHER_CMD_DOWN;
    
    if (set_fire)
      msg |= MISSILE_LAUNCHER_CMD_FIRE;

    missile_do(control, msg, device_type);
    
    if (set_stop) {
      usleep(delay * 1000);
      missile_do(control, MISSILE_LAUNCHER_CMD_STOP, device_type);
    }

    break;

  case DEVICE_TYPE_CIRCUS_CANNON:

    if (set_left)
      msg |= CIRCUS_CANNON_CMD_LEFT;
    
    if (set_right)
      msg |= CIRCUS_CANNON_CMD_RIGHT;
    
    if (set_up)
      msg |= CIRCUS_CANNON_CMD_UP;
    
    if (set_down)
      msg |= CIRCUS_CANNON_CMD_DOWN;
    
    if (set_fire)
      msg |= CIRCUS_CANNON_CMD_FIRE;

    missile_do(control, msg, device_type);

    if (set_stop) {
      usleep(delay * 1000);
      missile_do(control, CIRCUS_CANNON_CMD_STOP, device_type);
    }
      
    break;
    
  default:
    printf("Device Type (%d) not implemented, please do it!\n",
	   device_type);
    return -1;
    
  }

  missile_usb_destroy(control);  

  //---------------------------------------------------------------------------

  return 0;
}
int HandleKeyboardEvent(struct input_event *ev, int events, int device_type) {

  char msg = 0x00;
  int i;

  int key_pressed = 0;

  for (i = 0; i < events; i++) {

    switch (ev[i].type) {

    case EV_KEY:

      switch (device_type) {
	
      case DEVICE_TYPE_MISSILE_LAUNCHER:
	
	switch (ev[i].code) {
	  
	case KEY_UP:
	  msg |= MISSILE_LAUNCHER_CMD_UP;
	  key_pressed = 1;
	  break;
	  
	case KEY_DOWN:
	  msg |= MISSILE_LAUNCHER_CMD_DOWN;
	  key_pressed = 1;
	  break;
	  
	case KEY_LEFT:
	  msg |= MISSILE_LAUNCHER_CMD_LEFT;
	  key_pressed = 1;
	  break;
	  
	case KEY_RIGHT:
	  msg |= MISSILE_LAUNCHER_CMD_RIGHT;
	  key_pressed = 1;
	  break;
	  
	case KEY_F:
	  msg |= MISSILE_LAUNCHER_CMD_FIRE;
	  key_pressed = 1;
	  break;
	  
	case KEY_S: case KEY_SPACE:
	  msg |= MISSILE_LAUNCHER_CMD_STOP;
	  key_pressed = 1;
	  break;
	  
	default:
	  if (debug_level > 1000)
	    fprintf(stderr, "Unused Event Code: %d\n", ev[i].code);
	}

	break;

      case DEVICE_TYPE_CIRCUS_CANNON:

	switch (ev[i].code) {
	  
	case KEY_UP:
	  msg |= CIRCUS_CANNON_CMD_UP;
	  key_pressed = 1;
	  break;
	  
	case KEY_DOWN:
	  msg |= CIRCUS_CANNON_CMD_DOWN;
	  key_pressed = 1;
	  break;
	  
	case KEY_LEFT:
	  msg |= CIRCUS_CANNON_CMD_LEFT;
	  key_pressed = 1;
	  break;
	  
	case KEY_RIGHT:
	  msg |= CIRCUS_CANNON_CMD_RIGHT;
	  key_pressed = 1;
	  break;
	  
	case KEY_F:
	  msg |= CIRCUS_CANNON_CMD_FIRE;
	  key_pressed = 1;
	  break;
	  
	case KEY_S: case KEY_SPACE:
	  msg |= CIRCUS_CANNON_CMD_STOP;
	  key_pressed = 1;
	  break;
	  
	default:
	  if (debug_level > 1000)
	    fprintf(stderr, "Unused Event Code: %d\n", ev[i].code);
	}

	break;
	
      default:
	printf("Device Type (%d) not implemented, please do it!\n",
	       device_type);
	return -1;

      } // switch (device_type)
      
      break;
	
    default:
      if (debug_level > 1000)
	fprintf(stderr, "Unused Event Type: %d\n", ev[i].type);
    }

  }

  if (key_pressed) {
    if (debug_level)
      fprintf(stderr, "msg %d\n", msg);
    missile_do(control, msg, device_type);
  }
  
  return 0;
}
int main(int argc, char **argv) {
  setbuf(stdout, NULL);
  const char *optstring = "hnFLRUDS:";

  int o;
  int delay = 0;

  int device_type = 1;

  unsigned int set_fire = 0, set_left = 0, set_right = 0;
  unsigned int set_up = 0, set_down = 0, set_stop = 0, set_net = 0;

  //---------------------------------------------------------------------------
  // Get args

  o = getopt(argc, argv, optstring);

  while (o != -1) {
    switch (o) {

    case 'h':
      usage(argv[0]);
      return 0;

    case 'n':
      set_net = 1;
      break;

    case 'F':
      set_fire = 1;
      break;

    case 'U':
      set_up = 1;
      break;

    case 'D':
      set_down = 1;
      break;

    case 'L':
      set_left = 1;
      break;

    case 'R':
      set_right = 1;
      break;

    case 'S':
      set_stop = 1;
      delay = atoi(optarg);
      break;

    }
    o = getopt(argc, argv, optstring);
  }

  //---------------------------------------------------------------------------

  if (missile_usb_initialise() != 0) {
    fprintf(stderr, "missile_usb_initalise failed: %s\n", strerror(errno));
    return -1;
  }
  
  control = missile_usb_create(debug_level, USB_TIMEOUT);
  if (control == NULL) {
    fprintf(stderr, "missile_usb_create() failed\n");
    return -1;
  }
  
  if (missile_usb_finddevice(control, 0, device_type) != 0) {
    fprintf(stderr, "USBMissileLauncher device not found\n");
    return -1;
  }

  //---------------------------------------------------------------------------

  char msg = 0x00;

  switch (device_type) {
    
  case DEVICE_TYPE_MISSILE_LAUNCHER:
  
    if (set_left)
      msg |= MISSILE_LAUNCHER_CMD_LEFT;
    
    if (set_right)
      msg |= MISSILE_LAUNCHER_CMD_RIGHT;
    
    if (set_up)
      msg |= MISSILE_LAUNCHER_CMD_UP;
    
    if (set_down)
      msg |= MISSILE_LAUNCHER_CMD_DOWN;
    
    if (set_fire)
      msg |= MISSILE_LAUNCHER_CMD_FIRE;

    missile_do(control, msg, device_type);
    
    if (set_stop) {
      usleep(delay * 1000);
      missile_do(control, MISSILE_LAUNCHER_CMD_STOP, device_type);
    }

    break;
    
  default:
    printf("Device Type (%d) not implemented, please do it!\n",
     device_type);
    return -1;
    
  }
  
  if(set_net){
  //UDPServer();
  TCPServer();
  }
  

  missile_usb_destroy(control);  

  //---------------------------------------------------------------------------

  return 0;
}
void *connection_handler(void *argss)
{
  struct myStruct *client = (struct myStruct*)argss;

  int sock = (*client).socket;
  int read_size;
  char *reply_message, client_message[512];

  char *up = "U";
  char *down = "D";
  char *left = "L";
  char *right = "R";
  char *fire = "F";
  char *stop = "S";
  char *quit = "Q";

  char msg = 0x00;
  int device_type = 1;

  reply_message = "Welcome to Missile Launcher Server.\n\n\rThe following commands are accepted: U, D, L, R, F, S, Q\n\r\tU - UP\n\r\tD - Down\n\r\tL - Left\n\r\tR - Right\n\r\tF - Fire\n\r\tS - Stop\n\r\tQ - Quit\n\n\rPlay nice now :)";
  if( write(sock, reply_message, strlen(reply_message)) == -1 ){
    printf("Error sending message\n");
  }

  while( (read_size = recv(sock , client_message , 512 , 0)) > 0 )
  {
    //Send the message back to client
    reply_message = "\n\r";
    if( write(sock, reply_message, strlen(reply_message)) == -1 ){
      printf("Error sending message\n");
    }

    printf("Received command %s from %s\n", client_message, (*client).ip);     
    
    if (strncmp(client_message, left, strlen(left)) == 0){
      msg = MISSILE_LAUNCHER_CMD_LEFT;
    
    }else if (strncmp(client_message, right, strlen(right)) == 0){
      msg = MISSILE_LAUNCHER_CMD_RIGHT;
    
    }else if (strncmp(client_message, up, strlen(up)) == 0){
      msg = MISSILE_LAUNCHER_CMD_UP;
    
    }else if (strncmp(client_message, down, strlen(down)) == 0){
      msg = MISSILE_LAUNCHER_CMD_DOWN;
    
    }else if (strncmp(client_message, fire, strlen(fire)) == 0){
      msg = MISSILE_LAUNCHER_CMD_FIRE;
      
    }else if (strncmp(client_message, stop, strlen(stop)) == 0){
      msg = MISSILE_LAUNCHER_CMD_STOP;

    }else if (strncmp(client_message, quit, strlen(quit)) == 0){
      read_size = 0;
      break;
    }

    
    missile_do(control, msg, device_type);
    usleep(300 * 1000);
    missile_do(control, MISSILE_LAUNCHER_CMD_STOP, device_type);

    msg = 0x00;
    memset(client_message, 0, sizeof(client_message));
  }

   
  if(read_size == 0)
  {
    printf("Client %s disconnected.\n", (*client).ip);
    fflush(stdout);
  }
  else if(read_size == -1)
  {
    perror("Error, recv failed!");
  }
       
  //Free the socket pointer
  close(sock);
  //free((*client).socket);
   
  return 0;
}