>>18817
Pretty much this. A "castle town" is about the largest gated community you could build.
Inside of the town is highly discriminatory and has strict standards, this is where the town's shareholders and main contracts live.
Outside of the town is pretty much open borders for whoever wants to leech off of the main economy, most of them aren't allowed in the town.
The simplest example I can think of is honestly Skyrim where the guards wouldn't let the lizard or cat things into the city.