static void start_client(GtkWidget *widget)
{
  struct sockaddr_in address;
  struct hostent *hostinfo;
  struct itimerval timeout;
  struct sigaction signalact, old_signalact;

  gint result;
  gchar addr_dq[16], host_alias[41], msg[41];
  gint i=9;
  const gchar *lfs = "Looking for server: ";

  if(isys_server>-1)
   {
    gethostname(host_alias, sizeof(host_alias)-1);
    if(strlen(host_alias)<21)
     {
      hostinfo = gethostbyname(host_alias);
      strcpy(addr_dq,
             inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0]));
      address.sin_addr.s_addr = inet_addr(addr_dq);
      address.sin_family = AF_INET;
      address.sin_port = htons(9734);
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if(sockfd==-1)
       perror("kb-socket");
      else
       {
        result = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
        if(result==-1)
         {
          perror("kb-connect");
          close(sockfd);
          sockfd = -1;
         }
       }
     }
    else
     {
      strcpy(msg, "Host alias name is too long!");
      boxed_message (msg, 2000, YELLOW);
      g_timeout_add (2000, connect_delay, widget);
      gtk_main ();
      sockfd = -1;
     }
   }
  else
   {
    sethostent(TRUE);
    sigaction(SIGALRM, NULL, &old_signalact);
    signalact.sa_handler = alarm_handler;
    signalact.sa_flags = 0;
    sigemptyset(&signalact.sa_mask);
    sigaction(SIGALRM, &signalact, NULL);
    while((hostinfo = gethostent()))
     if(hostinfo->h_aliases[0])
      if(kb_index (hostinfo->h_aliases[0], "localhost")<0)
       {
        strcpy(addr_dq,
               inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0]));
        if(kb_index (addr_dq, "127"))
         {
          strcpy(msg, lfs);
          strcat(msg, hostinfo->h_aliases[0]);
          boxed_message (msg, 1000, i%15+1);
          i++;
          g_timeout_add (GTK_TIME_DELAY, connect_delay, widget);
          gtk_main ();
          address.sin_addr.s_addr = inet_addr(addr_dq);
          address.sin_family = AF_INET;
          address.sin_port = htons(9734);
          sockfd = socket(AF_INET, SOCK_STREAM, 0);
          timeout.it_interval.tv_sec = 0;
          timeout.it_interval.tv_usec = 0;
          timeout.it_value.tv_sec = 2;
          timeout.it_value.tv_usec = 0;
          setitimer(ITIMER_REAL, &timeout, NULL);
          result = connect(sockfd, (struct sockaddr *)&address,
                           sizeof(address));
          timeout.it_value.tv_sec = 0;
          setitimer(ITIMER_REAL, &timeout, NULL);
          g_timeout_add (GTK_TIME_DELAY, connect_delay, widget);
          gtk_main ();
          if(result!=-1)
           break;
          close(sockfd);
          sockfd = -1;
         }
       }
    sigaction(SIGALRM, &old_signalact, NULL);
    endhostent();
   }
  if(sockfd==-1)
   {
    message_box (widget, "Cannot connect to network server!",
                 !REDRAW, CLEAR);
    play_bells (MESSAGE_SOUND);
   }
  else
   {
    g_thread_create ((GThreadFunc)run_client, widget, FALSE, NULL);
    tip_message (widget, TIP_REDRAW);
   }
}

static gint connect_delay(gpointer widget)
{
  gtk_main_quit ();
  return FALSE;
}

static void alarm_handler(gint sig)
{
//-----handles SIGALRM-----
}

static gint run_client(gpointer da)
{
  gchar line_in[NET_LENGTH];
  gint iread, nbytes;

  while(TRUE)
   {
    nbytes = 0;
    while(nbytes<NET_LENGTH)
     {
      iread = read(sockfd, line_in+nbytes, NET_LENGTH-nbytes);
      if(iread>0)
       nbytes += iread;
      else
       {
        printf("iread=%d fd=%d\n", iread, sockfd);
        net_num = -2;
        close(sockfd);
        sockfd = -1;
        return FALSE;
       }
     }
    strcpy(net_array[(net_num+1)%NET_LINES], line_in);
    net_num++;
   }
  return FALSE;
}

static void process_network_command(gpointer da)
{
  gint i, j, na_num, net_hold, iat, nccv, ibl, nfc, ntc, ici, izi, socc;
  gint lcna, lcnam;
  gint new_from_client[6500], new_to_client[6500];
  gchar msg_line[NET_LENGTH], qso_char[5];
  gchar nccv_char[3], contest_line[76];
  gchar client_date[7], client_time[7], client_call[14];
  static gchar client_n_array[6500][NET_LENGTH];
  static gint cmds_handled=-1;
  static gint iuc=0;
  static gint icc_client=0;
  static gint idc=0;
  static gint ided_client=0;

  net_hold = net_num;
  if(net_hold==-2)
   {
    message_box (da, "Lost connection to network server!",
                 !REDRAW, CLEAR);
    play_bells (MESSAGE_SOUND);
    cmds_handled = -1;
    net_num = -1;
   }
  else
   {
    ici = countries.icountry_index;
    izi = countries.izone_index;
    while(net_hold>cmds_handled)
     {
      cmds_handled++;
      na_num = cmds_handled%NET_LINES;
      switch (net_array[na_num][0])
       {
        case 'C' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         ccv.number_of_clients = atoi(msg_line);
         break;
        case 'c' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         ibl = kb_index (msg_line, " ");
         concat(nccv_char, msg_line, 0, ibl-1, COPY);
         nccv = atoi(nccv_char);
         concat(ccv.client_alias[nccv], msg_line, ibl+1,
                strlen(msg_line)-1, COPY);
         break;
        case 'A' :
        case 'D' :
        case 'H' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         if(delete_from_network (msg_line, da))
          {
           scroll_log (da);
           cyan_bar (da);
           if(idisplay_type==IDT_SUMMARY && !ireset_display)
            {
             rate_box (da, !REDRAW);
             summary_box (da, !REDRAW);
             if(isel_contest==WW)
              zone_box (da, !REDRAW);
            }
          }
         else
          write_ded_file (6498, "D", FALSE);
         search_tree (icall_count+1, icall_count);
         countries.icountry_index = ici;
         countries.izone_index = izi;
         break;
        case 'B' :
        case 'E' :
        case 'I' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         fill_cr_array_all (msg_line, 6498);
         write_ded_file (6498, "E", FALSE);
         break;
        case 'b' :
        case 'e' :
        case 'i' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         fill_cr_array_all (msg_line, 6497);
         write_ded_file (6497, "e", FALSE);
         if(edit_from_network (da))
          {
           scroll_log (da);
           cyan_bar (da);
           if(idisplay_type==IDT_SUMMARY && !ireset_display)
            {
             rate_box (da, !REDRAW);
             summary_box (da, !REDRAW);
             if(isel_contest==WW)
              zone_box (da, !REDRAW);
            }
          }
         search_tree (icall_count+1, icall_count);
         countries.icountry_index = ici;
         countries.izone_index = izi;
         break;
        case 'F' :
         idc = 0;
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         ided_client = atoi(msg_line);
         read_ded_file ();
         if(!ided_client)
          {
           for(i=1; i<ded_num; i+=2)
            if(ded_array[i][0]=='D')
             send_to_server (ded_array[i+1], "A");
            else if(ded_array[i][0]=='E')
             send_to_server (ded_array[i+1], "B");
            else if(ded_array[i][0]=='e')
             send_to_server (ded_array[i+1], "b");
           send_to_server ("", "G");
          }
         break;
        case 'f' :
         idc++;
         concat(client_n_array[idc], net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         if(idc==ided_client)
          {
           for(i=1; i<=idc; i++)
            new_from_client[i] = 1;
           for(i=1; i<=ded_num; i++)
            new_to_client[i] = 1;
           for(i=1; i<idc; i+=2)
            for(j=1; j<ded_num; j+=2)
             if(!strcmp(client_n_array[i], ded_array[j]) &&
                !strcmp(client_n_array[i+1], ded_array[j+1]))
              {
               if(client_n_array[i][0]=='D')
                {
                 new_from_client[i] = 0;
                 new_from_client[i+1] = 0;
                 new_to_client[j] = 0;
                 new_to_client[j+1] = 0;
                 break;
                }
               else if(client_n_array[i][0]=='E')
                {
                 if(!strcmp(client_n_array[i+2], ded_array[j+2]) &&
                    !strcmp(client_n_array[i+3], ded_array[j+3]))
                  {
                   new_from_client[i] = 0;
                   new_from_client[i+1] = 0;
                   new_from_client[i+2] = 0;
                   new_from_client[i+3] = 0;
                   new_to_client[j] = 0;
                   new_to_client[j+1] = 0;
                   new_to_client[j+2] = 0;
                   new_to_client[j+3] = 0;
                   i+=2;
                   break;
                  }
                }
              }
           for(j=1; j<ded_num; j+=2)
            if(new_to_client[j])
             {
              if(ded_array[j][0]=='D')
               send_to_server (ded_array[j+1], "A");
              else if(ded_array[j][0]=='E')
               send_to_server (ded_array[j+1], "B");
              else if(ded_array[j][0]=='e')
               send_to_server (ded_array[j+1], "b");
             }
           for(i=1; i<idc; i+=2)
            if(new_from_client[i])
             {
              if(client_n_array[i][0]=='D')
               {
                send_to_server (client_n_array[i+1], "H");
                strcpy(msg_line, client_n_array[i+1]);
                if(delete_from_network (msg_line, da))
                 {
                  scroll_log (da);
                  cyan_bar (da);
                  if(idisplay_type==IDT_SUMMARY && !ireset_display)
                   {
                    rate_box (da, !REDRAW);
                    summary_box (da, !REDRAW);
                    if(isel_contest==WW)
                     zone_box (da, !REDRAW);
                   }
                 }
                else
                 write_ded_file (6498, "D", FALSE);
                search_tree (icall_count+1, icall_count);
                countries.icountry_index = ici;
                countries.izone_index = izi;
               }
              else if(client_n_array[i][0]=='E')
               {
                send_to_server (client_n_array[i+1], "I");
                strcpy(msg_line, client_n_array[i+1]);
                fill_cr_array_all (msg_line, 6498);
                write_ded_file (6498, "E", FALSE);
               }
              else if(client_n_array[i][0]=='e')
               {
                send_to_server (client_n_array[i+1], "i");
                strcpy(msg_line, client_n_array[i+1]);
                fill_cr_array_all (msg_line, 6497);
                write_ded_file (6497, "e", FALSE);
                if(edit_from_network (da))
                 {
                  scroll_log (da);
                  cyan_bar (da);
                  if(idisplay_type==IDT_SUMMARY && !ireset_display)
                   {
                    rate_box (da, !REDRAW);
                    summary_box (da, !REDRAW);
                    if(isel_contest==WW)
                     zone_box (da, !REDRAW);
                   }
                 }
                search_tree (icall_count+1, icall_count);
                countries.icountry_index = ici;
                countries.izone_index = izi;
               }
             }
           send_to_server ("", "G");
          }
         break;
        case 'M' :
         play_bells (MESSAGE_SOUND);
         iat = kb_index (net_array[na_num], "@");
         if(iat>-1)
          concat(msg_line, net_array[na_num], iat+2,
                 strlen(net_array[na_num])-1, COPY);
         else
          concat(msg_line, net_array[na_num], 2,
                 strlen(net_array[na_num])-1, COPY);
         network_message_box (da, msg_line, CLEAR);
         break;
        case 'm' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         network_message_box (da, msg_line, !CLEAR);
         break;
        case 'S' :
         read_ded_file ();
         int_to_char (ded_num, qso_char);
         send_to_server (qso_char, "F");
         for(i=1; i<=ded_num; i++)
          send_to_server (ded_array[i], "f");
         break;
        case 'G' :
         int_to_char (icall_count, qso_char);
         send_to_server (qso_char, "U");
         for(i=1; i<=icall_count; i++)
          {
           make_contest_line (contest_line, i);
           send_to_server (contest_line, "u");
          }
         break;
        case 't' :
        case 'v' :
         sort_cr_array (da);
         if(idisplay_type>IDT_SET)
          {
           if(!ireset_display)
            update_display (da, !REDRAW);
           else
            {
             scroll_log (da);
             cyan_bar (da);
            }
          }
         search_tree (icall_count+1, icall_count);
         countries.icountry_index = ici;
         countries.izone_index = izi;
         break;
        case 'U' :
         iuc = 0;
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         icc_client = atoi(msg_line);
         if(!icc_client)
          {
           for(i=1; i<=icall_count; i++)
            {
             make_contest_line (contest_line, i);
             send_to_server (contest_line, "V");
            }
           strcpy(contest_line, "");
           send_to_server (contest_line, "v");
           strcpy(contest_line, "");
           send_to_server (contest_line, "s");
          }
         break;
        case 'u' :
         iuc++;
         concat(client_n_array[iuc], net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         if(iuc==icc_client)
          {
           for(i=1; i<=iuc; i++)
            new_from_client[i] = 1;
           for(i=1; i<=icall_count; i++)
            new_to_client[i] = 1;
           for(i=1; i<=iuc; i++)
            {
             concat(client_date, client_n_array[i], 9, 14, COPY);
             concat(client_time, client_n_array[i], 15, 20, COPY);
             concat(client_call, client_n_array[i], 21, 33, COPY);
             right_blank_unpad (client_call);
             for(j=1; j<=icall_count; j++)
              if(!strcmp(client_date, cr_array[j].date) &&
                 !strcmp(client_time, cr_array[j].time) &&
                 !strcmp(client_call, cr_array[j].call))
               {
                new_from_client[i] = 0;
                new_to_client[j] = 0;
                break;
               }
            }
           ntc = FALSE;
           for(j=1; j<=icall_count; j++)
            if(new_to_client[j])
             {
              ntc = TRUE;
              make_contest_line (contest_line, j);
              send_to_server (contest_line, "V");
             }
           if(ntc)
            {
             strcpy(contest_line, "");
             send_to_server (contest_line, "v");
            }
           nfc = FALSE;
           for(i=1; i<=iuc; i++)
            if(new_from_client[i])
             {
              nfc = TRUE;
              send_to_server (client_n_array[i], "T");
              work_from_network (client_n_array[i], da);
             }
           if(nfc)
            {
             strcpy(contest_line, "");
             send_to_server (contest_line, "t");
             sort_cr_array (da);
             if(idisplay_type>IDT_SET)
              {
               if(!ireset_display)
                update_display (da, !REDRAW);
               else
                {
                 scroll_log (da);
                 cyan_bar (da);
                }
              }
             search_tree (icall_count+1, icall_count);
             countries.icountry_index = ici;
             countries.izone_index = izi;
            }
           strcpy(contest_line, "");
           send_to_server (contest_line, "s");
          }
         break;
        case 'Q' :
         if(pbm.spot_num>-1)
          {
           socc = sizeof(client_call);
           if(strlen(pbm.call[PBM_LINES-1]))
            for(i=pbm.spot_num+1; i<PBM_LINES; i++)
             if(idelta_time (pbm.date[i], kb_dt.date,
                pbm.time[i], kb_dt.time)<MAX_SPOT_AGE)
              {
               strcpy(client_call, pbm.call[i]);
               right_blank_pad (client_call, socc);
               strcpy(contest_line, client_call);
               strcat(contest_line, pbm.frequency[i]);
               strcat(contest_line, pbm.date[i]);
               strcat(contest_line, pbm.time[i]);
               send_to_server (contest_line, "q");
              }
           for(i=0; i<=pbm.spot_num; i++)
            if(idelta_time (pbm.date[i], kb_dt.date,
               pbm.time[i], kb_dt.time)<MAX_SPOT_AGE)
             {
              strcpy(client_call, pbm.call[i]);
              right_blank_pad (client_call, socc);
              strcpy(contest_line, client_call);
              strcat(contest_line, pbm.frequency[i]);
              strcat(contest_line, pbm.date[i]);
              strcat(contest_line, pbm.time[i]);
              send_to_server (contest_line, "q");
             }
          }
         strcpy(contest_line, "");
         send_to_server (contest_line, "r");
         break;
        case 'q' :
         pbm.spot_num++;
         pbm.spot_num %= PBM_LINES;
         concat(pbm.call[pbm.spot_num], net_array[na_num], 2, 14, COPY);
         concat(pbm.frequency[pbm.spot_num], net_array[na_num], 15, 21, COPY);
         concat(pbm.date[pbm.spot_num], net_array[na_num], 22, 27, COPY);
         concat(pbm.time[pbm.spot_num], net_array[na_num], 28, 33, COPY);
         right_blank_unpad (pbm.call[pbm.spot_num]);
         break;
        case 'R' :
         if(idisplay_type==IDT_PACKET && !ireset_display)
          update_display(da, !REDRAW);
         break;
        case 'T' :
        case 'V' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         work_from_network (msg_line, da);
         break;
        case 'W' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         work_from_network (msg_line, da);
         if(idisplay_type>IDT_SET)
          {
           scroll_log (da);
           cyan_bar (da);
           if(idisplay_type==IDT_SUMMARY && !ireset_display)
            {
             rate_box (da, !REDRAW);
             summary_box (da, !REDRAW);
             if(isel_contest==WW)
              zone_box (da, !REDRAW);
            }
          }
         search_tree (icall_count+1, icall_count);
         countries.icountry_index = ici;
         countries.izone_index = izi;
         break;
        case 'n' :
         concat(msg_line, net_array[na_num], 2,
                strlen(net_array[na_num])-1, COPY);
         isys_packet = atoi(msg_line);
         break;
        case 'p' :
         lcna = strlen(net_array[na_num])-1;
         lcnam = lcna>77 ? 77 : lcna;
         packet_num++;
         packet_num %= (17-log_lines[IDT_PACKET]);
         concat(packet_array[packet_num], net_array[na_num],
                2, lcnam, COPY);
         if(!kb_index (packet_array[packet_num], "DX de "))
          {
           if(!filter_spot ())
            {
             pbm.spot_num++;
             pbm.spot_num %= PBM_LINES;
             fill_kb_dt ();
             strcpy(pbm.date[pbm.spot_num], kb_dt.date);
             strcpy(pbm.time[pbm.spot_num], kb_dt.time);
             concat(pbm.frequency[pbm.spot_num], packet_array[packet_num],
                    17, 23, COPY);
             concat(pbm.call[pbm.spot_num], packet_array[packet_num],
                    26, 37, COPY);
             right_blank_unpad (pbm.call[pbm.spot_num]);
            }
          }
         else if(lcna>lcnam)
          {
           for(i=78; i<=lcna; i++)
            if(net_array[na_num][i]!='\n' && net_array[na_num][i]!=' ')
             {
              packet_num++;
              packet_num %= (17-log_lines[IDT_PACKET]);
              concat(packet_array[packet_num], net_array[na_num],
                     78, lcna, COPY);
              break;
             }
          }
         if(idisplay_type==IDT_PACKET && !ipage_screen[0][IDT_PACKET] &&
            !ireset_display)
          update_display(da, !REDRAW);
         break;
        default :
         message_box (da, "Help Me!", !REDRAW, CLEAR);
         message_box (da, net_array[na_num], !REDRAW, !CLEAR);
         play_bells (BAD_KEY_SOUND);
       }
     }
   }
}

static void work_from_network(const gchar *msg_line, gpointer da)
{
  gint i, new_mult;
  gchar qso_char[5];

  for(i=icall_count+4; i>icall_count; i--)
   {
    cr_array[i+1] = cr_array[i];
    int_to_char (i+1, qso_char);
    left_blank_pad (qso_char, sizeof(qso_char));
    strcpy(cr_array[i+1].qso, qso_char);
   }
  fill_cr_array_all (msg_line, icall_count+1);
  int_to_char (icall_count+1, qso_char);
  left_blank_pad (qso_char, sizeof(qso_char));
  strcpy(cr_array[icall_count+1].qso, qso_char);
  if(check_for_dupe (icall_count+1, icall_count, FALSE, da))
   strcpy(cr_array[icall_count+1].dupe, "*");
  else
   strcpy(cr_array[icall_count+1].dupe, "");
  insert_tree (icall_count+1, icall_count);
  new_mult = check_for_mult (icall_count+1, RESTART);
  increment_totals (icall_count+1);
  icall_count++;
  iscroll_log = icall_count;
  write_call_record (FALSE);
}

static gint delete_from_network(const gchar *msg_line, gpointer da)
{
  gint i;
  gint dfn=FALSE;

  fill_cr_array_all (msg_line, 6498);
  search_tree (6498, icall_count);
  if(call_tree.cmatch>0)
   for(i=call_tree.clow; i<=call_tree.chigh; i++)
    {
     if(!strcmp(cr_array[call_tree.sort_order[i]].date,
        cr_array[6498].date) &&
        !strcmp(cr_array[call_tree.sort_order[i]].time,
        cr_array[6498].time) &&
        !strcmp(cr_array[call_tree.sort_order[i]].call,
        cr_array[6498].call) &&
        cr_array[call_tree.sort_order[i]].iband==cr_array[6498].iband &&
        cr_array[call_tree.sort_order[i]].mode==cr_array[6498].mode)
      {
       delete_log_entry (da, call_tree.sort_order[i], FALSE);
       iscroll_log = icall_count;
       dfn = TRUE;
       break;
      }
    }
  return dfn;
}

static gint edit_from_network(gpointer da)
{
  gint i, iqso;
  gint efn=FALSE;
  gchar qso_char[5];

  search_tree (6498, icall_count);
  if(call_tree.cmatch>0)
   for(i=call_tree.clow; i<=call_tree.chigh; i++)
    {
     if(!strcmp(cr_array[call_tree.sort_order[i]].date,
        cr_array[6498].date) &&
        !strcmp(cr_array[call_tree.sort_order[i]].time,
        cr_array[6498].time) &&
        !strcmp(cr_array[call_tree.sort_order[i]].call,
        cr_array[6498].call) &&
        cr_array[call_tree.sort_order[i]].iband==cr_array[6498].iband &&
        cr_array[call_tree.sort_order[i]].mode==cr_array[6498].mode)
      {
       strcpy(qso_char, cr_array[call_tree.sort_order[i]].qso);
       cr_array[call_tree.sort_order[i]] = cr_array[6497];
       strcpy(cr_array[call_tree.sort_order[i]].qso, qso_char);
       iqso = call_tree.sort_order[i];
       reset_arrays ();
       for(i=0; i<icall_count; i++)
        {
         if(check_for_dupe (i+1, i, FALSE, da))
          strcpy(cr_array[i+1].dupe, "*");
         insert_tree (i+1, i);
        }
       if(strcmp(cr_array[6498].rec_county, cr_array[iqso].rec_county))
        {
         search_tree (iqso, icall_count);
         for(i=call_tree.clow; i<=call_tree.chigh; i++)
          if(call_tree.sort_order[i]!=iqso)
           strcpy(cr_array[call_tree.sort_order[i]].rec_county,
                  cr_array[iqso].rec_county);
        }
       if(isel_contest==WW)
        {
         if((cr_array[6498].rec_num[3-mode] !=
            cr_array[iqso].rec_num[3-mode]) ||
            (cr_array[6498].rec_num[4-mode] !=
            cr_array[iqso].rec_num[4-mode]))
          {
           search_tree (iqso, icall_count);
           for(i=call_tree.clow; i<=call_tree.chigh; i++)
            if(call_tree.sort_order[i]!=iqso)
             {
              cr_array[call_tree.sort_order[i]].rec_num[3-mode] =
              cr_array[iqso].rec_num[3-mode];
              cr_array[call_tree.sort_order[i]].rec_num[4-mode] =
              cr_array[iqso].rec_num[4-mode];
             }
          }
        }
       for(i=1; i<=icall_count; i++)
        {
         check_for_mult (i, RESTART);
         fill_cr_array_pts (i);
         increment_totals (i);
        }
       rewrite_contest_file ();
       efn = TRUE;
       break;
      }
    }
  return efn;
}

static void send_to_server(const gchar *contest_line, const gchar *cmd_type)
{
  gint i, iwrite, nbytes;
  gchar line_out[NET_LENGTH];

  strcpy(line_out, cmd_type);
  strcat(line_out, " ");
  if(strlen(contest_line))
   strcat(line_out, contest_line);
  for(i=strlen(line_out); i<NET_LENGTH; i++)
   line_out[i] = '\0';
  nbytes = 0;
  while(nbytes<NET_LENGTH)
   {
    iwrite = write(sockfd, line_out+nbytes, NET_LENGTH-nbytes);
    if(iwrite>0)
     nbytes += iwrite;
    else
     {
      printf("iwrite=%d fd=%d\n", iwrite, sockfd);
      break;
     }
   }
}

static void sort_cr_array(gpointer da)
{
  register gint i;
  gint exchange;
  gchar qnum[5];
  struct call_record cr_hold;

  ialt_de = 'S';
  write_backup_file (contest_file_name);
  do
   {
    exchange = 0;
    for(i=icall_count; i>1; i--)
     if(idelta_time (cr_array[i].date, cr_array[i-1].date,
        cr_array[i].time, cr_array[i-1].time)>0)
      {
       cr_hold = cr_array[i-1];
       cr_array[i-1] = cr_array[i];
       cr_array[i] = cr_hold;
       exchange = 1;
      }
    for(i=2; i<=icall_count; i++)
     if(idelta_time (cr_array[i].date, cr_array[i-1].date,
        cr_array[i].time, cr_array[i-1].time)>0)
      {
       cr_hold = cr_array[i-1];
       cr_array[i-1] = cr_array[i];
       cr_array[i] = cr_hold;
       exchange = 1;
      }
   } while(exchange);
  for(i=1; i<=icall_count; i++)
   {
    int_to_char (i, qnum);
    left_blank_pad (qnum, sizeof(qnum));
    strcpy(cr_array[i].qso, qnum);
   }
  reset_arrays ();
  for(i=0; i<icall_count; i++)
   {
    if(check_for_dupe (i+1, i, FALSE, da))
     strcpy(cr_array[i+1].dupe, "*");
    insert_tree (i+1, i);
   }
  for(i=1; i<=icall_count; i++)
   {
    check_for_mult (i, RESTART);
    fill_cr_array_pts (i);
    increment_totals (i);
   }
  rewrite_contest_file ();
  ialt_de = FALSE;
}

static void network_message_box(GtkWidget *widget, const gchar *message_line,
                                gint iclear)
{
  GdkRectangle update_rect;
  gint i;
  gint row1=1, col1=1, row2=3, col2=50;
  static gchar message_array[2][50] = {"", ""};

  update_rect.x = col1*wcol;
  update_rect.y = row1*hrow;
  update_rect.width = (col2-col1-1)*wcol;
  update_rect.height = (row2-row1-1)*hrow;
  if(iclear)
   for(i=0; i<2; i++)
    message_array[i][0] = '\0';
  if(strlen(message_line))
   {
    strcpy(message_array[1], message_array[0]);
    strcpy(message_array[0], message_line);
   }
  if(strlen(message_array[1]))
   {
    draw_box (row1, col1, row2, col2, 2, LT_GRAY, BLACK,
              message_array[0], WHITE, "", LT_GRAY, SAVEPIX);
    draw_rectangle (&update_rect, GREEN, SAVEPIX);
    write_line (row1, col1+1, message_array[1], WHITE, SAVEPIX);
    gtk_widget_queue_draw_area (widget,
                                update_rect.x-wcol,
                                update_rect.y-hrow,
                                update_rect.width+2*wcol,
                                update_rect.height+2*hrow);
   }
}

static void send_msg_box(void)
{
  GdkRectangle update_rect;
  static SMBVars smbd = {
   .field_length = 46, .col1 = 17, .cursor_col = 0};
  gint row1=4, col1=16, row2=6, col2=63;
  gint idraw=TRUE;
  const gchar *title = "Send Network Message";
  const gchar *subtitle = "Send/Exit:Enter";

  if(sockfd>-1)
   {
    update_rect.x = col1*wcol;
    update_rect.y = row1*hrow;
    update_rect.width = (col2-col1-1)*wcol;
    update_rect.height = (row2-row1-1)*hrow;
    ireset_display = FALSE;
    update_display (main_drawing_area, REDRAW);
    main_cursor_type = CURSOR_OFF;
    ialt_box = 'M';
    smbd.row1 = row1+1;
    smbd.cursor_col = smbd.col1;
    smbd.done = FALSE;
    strcpy(smbd.field_line, "");
    g_signal_handler_block (main_window, main_key_handler_id);
    smbd.key_handler_id = g_signal_connect (G_OBJECT (main_window),
                          "key_press_event",
                          G_CALLBACK (smb_key_handler), &smbd);
    g_timeout_add (250, smb_cursor, &smbd);
    draw_box (row1, col1, row2, col2, 1, LT_GRAY, ORANGE,
              title, WHITE, subtitle, LT_GRAY, SAVEPIX);
    draw_shadow (main_drawing_area, row1, col1, row2, col2, SAVEPIX);
    draw_rectangle (&update_rect, ORANGE, SAVEPIX);
    if(insert_mode)
     color_bar (main_drawing_area, row1+1, col1+1, row1+1, col2-1, YELLOW);
    else
     black_bar (main_drawing_area, row1+1, col1+1, row1+1, col2-1);
    gtk_widget_queue_draw_area (main_drawing_area,
                                update_rect.x-idraw*wcol,
                                update_rect.y-idraw*hrow,
                                update_rect.width+idraw*2*wcol,
                                update_rect.height+idraw*2*hrow);
    gtk_main ();
   }
  else
   {
    play_bells (MESSAGE_SOUND);
    message_box (main_drawing_area, "to send message to network!",
                 !REDRAW, CLEAR);
    message_box (main_drawing_area, "Client must be running",
                 !REDRAW, !CLEAR);
   }
}

static gint smb_key_handler(GtkWidget *widget, GdkEventKey *event,
                            gpointer data)
{
  SMBVars *smbd;
  ESVars esd;
  guint ikey;
  gchar line_out[81], send_to_who[21];
  
  smbd = (SMBVars*)data;
  ikey = event->keyval;
  esd.ikey = ikey;
  esd.cursor_row = smbd->row1;
  esd.cursor_colp = &smbd->cursor_col;
  esd.field_col = smbd->col1;
  esd.field_length = smbd->field_length;
  esd.field_linep = smbd->field_line;
  switch (ikey)
   {
    case 65505 : /*left shift*/
    case 65506 : /*right shift*/
    case 65509 : /*caps lock*/
    case 65407 : /*num lock*/
     break;
    case 65361 : /*left_arrow*/
    case 65363 : /*right_arrow*/
    case 65360 : /*home*/
    case 65367 : /*end*/
    case 65535 : /*delete*/
    case 65288 : /*backspace*/
     edit_line (&esd);
     break;
    case 65379 : /*insert*/
     insert_mode = !insert_mode;
     edit_line (&esd);
     break;
    case 65293 : /*enter*/
     if(strlen(smbd->field_line))
      {
       strcpy(send_to_who, "ALL ");
       strcpy(line_out, send_to_who);
       strcat(line_out, "@ ");
       strcat(line_out, smbd->field_line);
       send_to_server (line_out, "M");
      }
     smbd->done = TRUE;
     ialt_box = FALSE;
     main_cursor_type = LINE_CURSOR;
     break;
    case 65307 : /*escape*/
     strcpy(smbd->field_line, "");
     edit_line (&esd);
     break;
    default :
     if(isprint_ui(ikey))
      edit_line (&esd);
     else
      play_bells (BAD_KEY_SOUND);
   }
  return TRUE;
}

static gint smb_cursor(gpointer data)
{
  SMBVars *smbd;
  ESVars esd;
  gint cursor_fg_color, cursor_bg_color;
  static gint iadd = 0;

  smbd = (SMBVars*)data;
  if(smbd->done)
   {
    g_signal_handler_disconnect (main_window, smbd->key_handler_id);
    g_signal_handler_unblock (main_window, main_key_handler_id);
    gtk_main_quit ();
    update_display (main_drawing_area, REDRAW);
    if(insert_mode)
     {
      esd.ikey = 65379;
      esd.cursor_row = cbv.cursor_row;
      esd.cursor_colp = &cbv.cursor_col;
      esd.field_col = cbv.col1[cbv.item];
      esd.field_length = cbv.field_length[cbv.item];
      set_esd_flp (icall_count+1, &esd);
      edit_line (&esd);
     }
    return FALSE;
   }
  iadd++;
  iadd %= 4;
  if(insert_mode)
   {
    cursor_fg_color = BLACK;
    cursor_bg_color = YELLOW;
   }
  else
   {
    cursor_fg_color = YELLOW;
    cursor_bg_color = BLACK;
   }
  cursor_control (main_drawing_area, iadd, LINE_CURSOR,
                  smbd->row1, smbd->cursor_col,
                  cursor_fg_color, cursor_bg_color);
  return TRUE;
}

static void send_packet_box(const gchar *spb_line, gint spb_spot)
{
  GdkRectangle update_rect;
  gchar pli[58];
  static SMBVars spbd = {
   .field_length = 57, .col1 = 12, .cursor_col = 0};
  gint row1=4, col1=11, row2=6, col2=69;
  gint idraw=TRUE;
  const gchar *title = "Send Packet Command";
  const gchar *subtitle = "Send/Exit:Enter";

  if(isys_packet>-1 && sockfd>-1)
   {
    strcpy(pli, spb_line);
    update_rect.x = col1*wcol;
    update_rect.y = row1*hrow;
    update_rect.width = (col2-col1-1)*wcol;
    update_rect.height = (row2-row1-1)*hrow;
    if(!spb_spot)
     {
      idisplay_type = IDT_PACKET;
      ipage_screen[0][idisplay_type] = 0;
     }
    ireset_display = FALSE;
    update_display (main_drawing_area, REDRAW);
    main_cursor_type = CURSOR_OFF;
    ialt_box = 'M';
    spbd.row1 = row1+1;
    spbd.cursor_col = spbd.col1+strlen(pli);
    spbd.done = FALSE;
    strcpy(spbd.field_line, pli);
    g_signal_handler_block (main_window, main_key_handler_id);
    spbd.key_handler_id = g_signal_connect (G_OBJECT (main_window),
                          "key_press_event",
                          G_CALLBACK (spb_key_handler), &spbd);
    g_timeout_add (250, spb_cursor, &spbd);
    draw_box (row1, col1, row2, col2, 1, LT_GRAY, ORANGE,
              title, WHITE, subtitle, LT_GRAY, SAVEPIX);
    draw_shadow (main_drawing_area, row1, col1, row2, col2, SAVEPIX);
    draw_rectangle (&update_rect, ORANGE, SAVEPIX);
    if(insert_mode)
     color_bar (main_drawing_area, row1+1, col1+1, row1+1, col2-1, YELLOW);
    else
     black_bar (main_drawing_area, row1+1, col1+1, row1+1, col2-1);
    write_line (row1, col1, pli, YELLOW, SAVEPIX);
    gtk_widget_queue_draw_area (main_drawing_area,
                                update_rect.x-idraw*wcol,
                                update_rect.y-idraw*hrow,
                                update_rect.width+idraw*2*wcol,
                                update_rect.height+idraw*2*hrow);
    gtk_main ();
   }
  else
   {
    play_bells (MESSAGE_SOUND);
    if(spb_spot)
     message_box (main_drawing_area, "to send a spot!",
                  !REDRAW, CLEAR);
    else
     message_box (main_drawing_area, "to send command!",
                  !REDRAW, CLEAR);
    message_box (main_drawing_area, "Packet must be running",
                 !REDRAW, !CLEAR);
   }
}

static gint spb_key_handler(GtkWidget *widget, GdkEventKey *event,
                            gpointer data)
{
  SMBVars *spbd;
  ESVars esd;
  guint ikey;
  gchar line_out[NET_LENGTH];
  const gchar *hf_bmf[2] = {
   "160-cw,80-cw,40-cw,20-cw,15-cw,10-cw,40-rtty,20-rtty,15-rtty",
   "160-ssb,80-ssb,40-ssb,20-ssb,15-ssb,10-ssb,40-cw,40-rtty"};
/*
  const gchar *v6_macros[6] = {"{ARRL-CW}", "{ARRL-SSB}",
                               "{CQWW-CW}", "{CQWW-SSB}",
                               "{CW-CONTEST-FREQ}", "{SSB-CONTEST-FREQ}"};
*/
  spbd = (SMBVars*)data;
  ikey = event->keyval;
  esd.ikey = ikey;
  esd.cursor_row = spbd->row1;
  esd.cursor_colp = &spbd->cursor_col;
  esd.field_col = spbd->col1;
  esd.field_length = spbd->field_length;
  esd.field_linep = spbd->field_line;
  switch (ikey)
   {
    case 65505 : /*left shift*/
    case 65506 : /*right shift*/
    case 65509 : /*caps lock*/
    case 65407 : /*num lock*/
     break;
    case 65361 : /*left_arrow*/
    case 65363 : /*right_arrow*/
    case 65360 : /*home*/
    case 65367 : /*end*/
    case 65535 : /*delete*/
    case 65288 : /*backspace*/
     edit_line (&esd);
     break;
    case 65379 : /*insert*/
     insert_mode = !insert_mode;
     edit_line (&esd);
     break;
    case 65293 : /*enter*/
     if(strlen(spbd->field_line))
      {
       if(!strcmp(spbd->field_line, "\\bmf"))
        {
         strcpy(line_out, "set/filter dxbm/pass ");
         strcat(line_out, hf_bmf[mode]);
        }
       else if(!strcmp(spbd->field_line, "\\csf"))
        {
         strcpy(line_out, "set/filter doc/pass k,ve");
         send_to_server (line_out, "P");
         strcpy(line_out,
          "set/filter dos/pass k1,k2,k3,ve1,ve2,ve3,ve9,vy2,vo1,vo2");
        }
       else if(!strcmp(spbd->field_line, "\\v6f"))
        {
         strcpy(line_out,
                "set/dx/filter not skimmer and (band=160 or band=80 or ");
         send_to_server (line_out, "O");
         strcpy(line_out,
                "band=40 or band=20 or band=15 or band=10)");
         send_to_server (line_out, "O");
         strcpy(line_out, " or call=");
         strcat(line_out, init_screen_choices[0]);
        }
       else if(!strcmp(spbd->field_line, "\\v6z"))
        {
         strcpy(line_out,
                "set/dx/filter not skimmer and (spottercqzone=2 or ");
         send_to_server (line_out, "O");
         strcpy(line_out,
                "spottercqzone=3 or spottercqzone=4 or spottercqzone=5)");
         send_to_server (line_out, "O");
         strcpy(line_out, " or call=");
         strcat(line_out, init_screen_choices[0]);
        }
/*
       else if(!strcmp(spbd->field_line, "\\v6q"))
        {
         strcpy(line_out,
                "set/dx/filter not skimmer and (spotterstate=ME or ");
         send_to_server (line_out, "O");
         strcpy(line_out,
                "spotterstate=NH or spotterstate=VT or spotterstate=MA or ");
         send_to_server (line_out, "O");
         sleep(1);
         strcpy(line_out,
                "spotterstate=CT or spotterstate=RI or spotterstate=NY or ");
         send_to_server (line_out, "O");
         strcpy(line_out,
                "spotterstate=NJ or spotterstate=ON or spotterstate=QC) or ");
         send_to_server (line_out, "O");
         strcpy(line_out, "call=");
         strcat(line_out, init_screen_choices[0]);
        }
       else if(!strcmp(spbd->field_line, "\\v6y"))
        {
         strcpy(line_out, "set/dx/filter not skimmer and ");
         switch (isel_contest)
          {
           case DX :
            strcat(line_out, v6_macros[mode]);
            break;
           case WW :
            strcat(line_out, v6_macros[mode+2]);
            break;
           case WPX :
           case SS :
            strcat(line_out, v6_macros[mode+4]);
            break;
          }
         strcat(line_out, " and (spotterstate=ME or ");
         send_to_server (line_out, "O");
         sleep(1);
         strcpy(line_out,
                "spotterstate=NH or spotterstate=VT or spotterstate=MA or ");
         send_to_server (line_out, "O");
         strcpy(line_out,
                "spotterstate=CT or spotterstate=RI or spotterstate=NY or ");
         send_to_server (line_out, "O");
         strcpy(line_out, "spotterstate=NJ or spotterstate=ON");
         switch (isel_contest)
          {
           case DX :
           case WW :
            if(strlen(init_screen_choices[0])<12)
             strcat(line_out, " or spotterstate=QC) or call=");
            else
             strcat(line_out, ") or call=");
            break;
           case WPX :
           case SS :
            strcat(line_out, ") or call=");
            break;
           default :
            strcat(line_out, " or spotterstate=QC) or call=");
          }
         strcat(line_out, init_screen_choices[0]);
         sleep(1);
        }
       else if(!strcmp(spbd->field_line, "\\v6x"))
        {
         strcpy(line_out, "set/dx/filter not skimmer and ");
         switch (isel_contest)
          {
           case DX :
            strcat(line_out, v6_macros[mode]);
            break;
           case WW :
            strcat(line_out, v6_macros[mode+2]);
            break;
           case WPX :
           case SS :
            strcat(line_out, v6_macros[mode+4]);
            break;
          }
         send_to_server (line_out, "O");
         sleep(1);
         strcpy(line_out, " and spottercqzone=5 or call=");
         strcat(line_out, init_screen_choices[0]);
        }
*/
       else if(!strcmp(spbd->field_line, "\\v6a"))
        {
         strcpy(line_out, "set/dx/filter not skimmer and ");
         strcat(line_out, "(spottercty=k or spottercty=ve) and ");
         send_to_server (line_out, "O");
         sleep(1);
         strcpy(line_out, "band<>30 and band<>17 and band<>12 or call=");
         strcat(line_out, init_screen_choices[0]);
        }
       else if(!strcmp(spbd->field_line, "\\v6b"))
        {
         strcpy(line_out, "set/dx/filter not skimmer and ");
         strcat(line_out, "(spottercty=k or spottercty=ve) and ");
         send_to_server (line_out, "O");
         sleep(1);
         strcpy(line_out,
                "band<>30 and band<>17 and band<>12 and freq<30000");
        }
       else
        strcpy(line_out, spbd->field_line);
       send_to_server (line_out, "P");
      }
     spbd->done = TRUE;
     ialt_box = FALSE;
     main_cursor_type = LINE_CURSOR;
     break;
    case 65307 : /*escape*/
     strcpy(spbd->field_line, "");
     edit_line (&esd);
     break;
    default :
     if(isprint_ui(ikey))
      edit_line (&esd);
     else
      play_bells (BAD_KEY_SOUND);
   }
  return TRUE;
}

static gint spb_cursor(gpointer data)
{
  SMBVars *spbd;
  ESVars esd;
  gint cursor_fg_color, cursor_bg_color;
  static gint iadd = 0;

  spbd = (SMBVars*)data;
  if(spbd->done)
   {
    g_signal_handler_disconnect (main_window, spbd->key_handler_id);
    g_signal_handler_unblock (main_window, main_key_handler_id);
    gtk_main_quit ();
    update_display (main_drawing_area, REDRAW);
    if(insert_mode)
     {
      esd.ikey = 65379;
      esd.cursor_row = cbv.cursor_row;
      esd.cursor_colp = &cbv.cursor_col;
      esd.field_col = cbv.col1[cbv.item];
      esd.field_length = cbv.field_length[cbv.item];
      set_esd_flp (icall_count+1, &esd);
      edit_line (&esd);
     }
    return FALSE;
   }
  iadd++;
  iadd %= 4;
  if(insert_mode)
   {
    cursor_fg_color = BLACK;
    cursor_bg_color = YELLOW;
   }
  else
   {
    cursor_fg_color = YELLOW;
    cursor_bg_color = BLACK;
   }
  cursor_control (main_drawing_area, iadd, LINE_CURSOR,
                  spbd->row1, spbd->cursor_col,
                  cursor_fg_color, cursor_bg_color);
  return TRUE;
}

static gint filter_spot(void)
{
  gint i, j;
  gint fspot=FALSE;
  const gchar *ca = "123";

  if(pbm.spot_filter)
   {
    fspot = TRUE;
    for(i=7; i<9; i++)
     for(j=0; j<3; j++)
      if(packet_array[packet_num][i]==ca[j])
       {
        fspot = FALSE;
        return fspot;
       }
   }
  return fspot;
}
