You're most likely hitting the cap for the number of open file descriptors on your operating system.
Linux
To see the number of descriptors currently in use (on a machine with the /proc filesystem), you can look at the /proc/sys/fs/file-max virtual device:
$ cat /proc/sys/fs/file-max
792823
or run:
sysctl fs.file-max
and to check the actual limit (-H is the hard limit, -S is the soft limit) run:
$ ulimit -Hn
4096
$ ulimit -Sn
1024
as the user for which you want to check the limit.
To change this on Linux, edit the /etc/sysctl.conf file:
fs.file-max = 100000
You'll need to log out and back in, or run sysctl -p for the changes to take effect.
To change the FD limit for a specific user, look in the /etc/security/limits.conf file and add lines like the following:
username soft nofile 4096
username hard nofile 10240
BSD
On BSD based systems, the sysctl variable is called kern.maxfiles:
sysctl kern.maxfiles
OS X
To check the limit on OS X run:
launchctl limit maxfiles
and look at the last two columns (soft and hard limits respectively).
To change the limits create a property list at: /Library/LaunchDaemons/limit.maxfiles.plist and add the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>65536</string>
<string>65536</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
Modifying the properties as necessary. Then create /Library/LaunchDaemons/limit.maxproc.plist with the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true />
<key>ServiceIPC</key>
<false />
</dict>
</plist>
then chown both files to root:wheel and chmod them to 644 (-rw-r--r--). Now restart or run launchctl limit to apply.
For older versions of OS X, this tutorial may help.
Solaris
In solaris, add a line like the following:
set rlim_fd_max=65536
to /etc/system (see this blog post for more info)