Im fairly new to c++ and this is my first time with winsocks.
Ive gathered the code and altered it to the best of my ability.
I have 2 problems.
1)When a conenction has been made it rejects it as it shauld but then i ask user if they wish to restart and it simply goes through all the errors and im not sure why.
2)The client side to connect to a computer only connects to the given http address. I want it to connect to an ip address from the address which made the connection before the 'hacker'.

The idea behind the program is there is a well known trojan out there and im fed up being scanned by it, so i decided to listen for a scan on port 27374 and when it is done see wether the 'hacker' is infected himself.It will then submit data to a website and after a certain amount of time there will be a databse with top ten scanners etc.Other ideas i have been given are to send certain packets forcing the computer to loose its conenction.
Here is the code below as far as i have got.

Any help wauld be appreciated.




// Link to -lwsock32 in Dev-C++ and winsock.lib
#include <winsock.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

void Socket();
void Scan();
void Shut();

int main()
{
printf("Sub shamer initiated \n");
printf("Listening For Connections \n");
Socket();
return 0;
}

void Socket()
{
int Mop;
WORD version = MAKEWORD(1, 1);
WSADATA wsaData;
int nRet;

// Init Winsock
WSAStartup(version, &wsaData);


// Create the server's socket
SOCKET listeningSocket;

listeningSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // Socket type
IPPROTO_TCP); // Protocol
if (listeningSocket == INVALID_SOCKET)
{
printf("Problem creating server. \n");

}


// Fill a SOCKADDR_IN struct with address information:
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = INADDR_ANY; // Since this is a server, any address will do
saServer.sin_port = htons(27374); // Convert int 8888 to a value for the port field


// Bind the socket to the local server address
nRet = bind(listeningSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR)
{
printf("Error creating server on local address. \n");

} // Begin listening



nRet = listen(listeningSocket, 10); // Up to 10 clients may be queued
if (nRet == SOCKET_ERROR)
{
printf("Error starting listening. \n");

}


// Create a client socket and wait for a connection
SOCKET theClient;

theClient = accept(listeningSocket,NULL,NULL);
if (theClient == INVALID_SOCKET)
{
printf("Error at accept \n");
}

cout<<"You had a connection do you wish to check the 'hacker'? ? \n Press 1 for yes and 2 for no \n";

cin >> Mop ;
if (Mop = 1)
{
Scan();
}


shutdown(theClient, SD_BOTH);

closesocket(theClient);
closesocket(listeningSocket);


// Shutdown Winsock
WSACleanup();
}

void Scan()
{
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;


// Init Winsock
WSAStartup(version, &wsaData);


// Get information about the server
LPHOSTENT lpHostEntry;

lpHostEntry = gethostbyname("www.hal-pc.org");
if (lpHostEntry == NULL)
{
printf("Error at gethostbyname()");

}

// Create client socket
SOCKET theSocket;

theSocket = socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // Socket type
IPPROTO_TCP); // Protocol
if (theSocket == INVALID_SOCKET)
{
printf("Error at socket()");

}


// Fill an address struct with the server's location
SOCKADDR_IN saServer;

saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Address of the server
saServer.sin_port = htons(27374);


// Connect to the specified address and port
nRet = connect(theSocket,
(LPSOCKADDR)&saServer, // Server address
sizeof(struct sockaddr)); // Length of address structure
if (nRet == SOCKET_ERROR)
{
printf("Error at connect()");

}

cout<<"Wauld you liek to restart the server and carry on listening? \n";
cout<<"1 for yes 2 for no . \n";
cin>>Restart;
if (Restart = 1)
{
Socket();
}


// Successfully connected!

closesocket(theSocket);

// Shutdown Winsock
WSACleanup();

}