Refactor resource into new module.

Also, make socket derive from resource, making it consistent with
the file class.