#include "inet.h"
#include "service.h"
#include "convert.h"

#include <iostream.h>
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
extern "C" {
           int bzero(char *, int);
           void bcopy(char *, char*, int);
           }

int main(int argc, char* argv[]) 
    {
    int PORT;
    int pid;
    int sockfd;
    struct sockaddr_in cli_addr, serv_addr;
    char * SERV_HOST_ADDR;
    Services *service = new Services();
 
    SERV_HOST_ADDR = argv[1];

    cout << "-----------------------------------------------------" << endl;
    for (PORT = 1; PORT < 1025; PORT ++)
        {
        if (sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0)
            {
            cout << "ERROR creating socket" << endl;
            exit(-1);
            }

        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = inet_addr(convert(SERV_HOST_ADDR));
        serv_addr.sin_port = htons(PORT);

        if(connect(sockfd,
                   (struct sockaddr *)&serv_addr,
                   sizeof(serv_addr)) < 0)
            close(sockfd);
        else
            {
            cout << "Port: " << PORT;
            cout << "   " << service->GetName(PORT) << " port is open." << endl;
            close(sockfd);
            }

        }
    cout << "--------------------------------------------------------------" << endl;
    return 0;
    }
