You are opening 20000 connections and you are closing none of them.
You should close connections in your for.

Code:
if(err>=0)
        {
            printf("\n%d is open",i);
        }
This is wrong. Values over zero may express an error, not a succesful connection.
You should really interpret this values and the print a correct output.
(hint: will return 0 on success).

You didn't specify the platform. For windows platform is necesary to call WSAStartup, to initialize the winsock library, otherwise you will receive errors from each call made to winsock's API.