int main(int argc, char *argv[])
{
    if (argc < 2) 
    {
        printf("usage: %s filename\n", argv[0]);
        exit(0);
    }
    
    char buffer[128];
    int read;
    
    FILE *file = fopen(argv[1], "wb");
    if ( file==NULL ){return 1;}
    
    
    //initialize networking
    //bind to ip 0.0.0.0:PORT
    IP ip;
    ip.i = 0;
    init_networking(ip, PORT);
    perror("Initialization");
    
    int connection;
    uint64_t timer = current_time();
    
    
    while(1)
    {
        Lossless_UDP();
        connection = incoming_connection();
        if(connection != -1)
        {
            if(is_connected(connection) == 2)
            {
                printf("Recieved the connection.\n");
                
            }
            break;
        }
        c_sleep(1);
    }
    
    timer = current_time();
    
    while(1)
    {
        //printconnection(0);
        Lossless_UDP();
        if(is_connected(connection) >= 2)
        {
            read = read_packet(connection, buffer);
            if(read != 0)
            {
               // printf("Recieved data.\n");
                if(!fwrite(buffer, read, 1, file))
                {
                        printf("file write error\n");
                }
            }
        }
        else
        {
            printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
            fclose(file);
            return 1;
        }
        c_sleep(1);
    }
        
    return 0;
}
int main(int argc, char *argv[])
{
    if (argc < 4) {
        printf("usage: %s ip port filename\n", argv[0]);
        exit(0);
    }
    
    uint8_t buffer[512];
    int read;
    
    FILE *file = fopen(argv[3], "rb");
    if (file == NULL)
      return 1;
    
    
    /* initialize networking */
    /* bind to ip 0.0.0.0:PORT */
    IP ip;
    ip.i = 0;
    init_networking(ip, PORT);
    perror("Initialization");
    IP_Port serverip;
    serverip.ip.i = inet_addr(argv[1]);
    serverip.port = htons(atoi(argv[2]));
    printip(serverip);
    int connection = new_connection(serverip);
    uint64_t timer = current_time();
    while (1) {
       /* printconnection(connection); */
        Lossless_UDP();
        if (is_connected(connection) == 3) {
            printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
            break;
        }
        if (is_connected(connection) == 0) {
            printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
            return 1;
        }
        c_sleep(1);
    }
    timer = current_time();
    
    
    /*read first part of file */
    read = fread(buffer, 1, 512, file);
    
    while (1) {
        /* printconnection(connection); */
        Lossless_UDP();
        if (is_connected(connection) == 3) {
            
            if (write_packet(connection, buffer, read)) {
               /* printf("Wrote data.\n"); */
                read = fread(buffer, 1, 512, file);

            }
            /* printf("%u\n", sendqueue(connection)); */
            if (sendqueue(connection) == 0) {
                if (read == 0) {
                    printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
                    break;
                }
            }
        }
        else {
            printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
            return 0;
        }
        /* c_sleep(1); */
    }
        
    return 0;
}