How to limit the number of child processes

I need a mechanism to fork child processes and all child processes should connect to a server.but the number of child processes should be limited(for ex:50)
Here's my pseudo, but I cant figure out how to limit the child process number. Should I use a semaphore? or what?

        
        for(;;)
        {
                pid=fork();
                switch(pid)
                {
                        case -1:
                                perror("fork");
                                break;
                        case 0:
                                Job();
                                sleep(5);
                                exit(0);
                                break;
                        default:
                                sleep(3);
                                break;
                }
        }

PS: i use Linux.

Instead of an infinite loop, for(n=0; n<50; n++)

Or do you mean these processes need to be continually replaced when they quit?

Yes, exactly that.
at any time, the max number of the childs will be 50, but the application will run forever.

I solved the problem by handling the SIGCHLD signal and counting the child processes number.