There is big mistake in your port scanner function ,because of that only it doesn't work for the next attempt . I too missed to debug it in the previous code.
Actually you tried to establish socket connection 20000 times.Some got succeed . For some time you opened some number of socket descriptors ,that exceeds the system limits.So it got fail the later iteration .You need to close the socket descriptor after checking the connect ion.
Here is the debugger port scanner function , now the problems will not occur ,try this.
struct sockaddr_in sa;
printf("Enter the ip : ");
if((new=connect(net,(struct sockaddr *)&sa,sizeof(sa)))>=0)
printf("%d is open:%d\n",i);
close(net); //Close the socket other wise it will exceed system limits
printf("now exit scanner\n");
Verify this output with your previous scanner outputs . You will see some big different .