Threading with sockets constantly segfaults

Posting new topic here with modified source code.
Server segfaults randomly after some synthetic tests.
Testing done with ab tool:
ab -c 10 -n 10000000 http://localhost:9000/

Even with 10 threads I get segfaults after couple of thousands requests.
The code is simple but I just cant get it where I did it wrong.

#coding: utf-8
################################################################################
require “socket”
require “thread”
################################################################################
scgid=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)
scgid.bind(Socket.pack_sockaddr_in(ARGV[1].to_i,ARGV[0]))
scgid.listen(10)

def handler scgi
Thread.new{
begin
request=scgi.sysread 1024
scgi.syswrite “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”
scgi.close
rescue Exception => err
STDOUT.syswrite err.to_s+“\n”
end
}
end

loop{
begin
handler scgid.accept[0]
rescue Exception => err
STDOUT.syswrite err.to_s+“\n”
end
}

Starting server: ruby loader.rb 127.0.0.1 9000

Appreciate any help. Thanks!

run it through gdb to see where the segfault occurs? any backtrace?