Auto add doors and windows

I am trying to get a door, window, hatch, etc to auto deploy when a builing block is added. However I am struggling to get them to connect to the parent successfully for all cases as they seem to be spawning in the worong socket location, I am wondering if there is an easier way to get the deployable to attach to the correct location? The code below works for windows, however when i rotate the wall, the window rotates also, but the code below doesnt work for doors or hatches, they just break when deployed. if i remove the female socket stuff the doors work, but then windows try an attach at the bottom and break, and none of this makes hatches work at all. I am not looking for a full blown solution, just wondering if I am even going in the right direction, or if there is an easier way that I am not seeing.

private void AutoDeployByPrefabName(BasePlayer player, BaseEntity parent, string shortPrefabName)
        {
            if(!deployables.ContainsKey(shortPrefabName))
            {
                Server.Broadcast($"Could not locate item from: {shortPrefabName}");
                return;
            }

            var itemId = deployables[shortPrefabName];

            var planner = new Planner();
            

            var deployable = Deployable.server.Find<Deployable>(itemId);



            var construction = PrefabAttribute.server.Find<Construction>(itemId);
            if (construction == null)
            {
                Server.Broadcast("construction null");
                return;
            }

            //Construction.Target target = new Construction.Target();
            //target.entity = GameManager.server.CreateEntity(construction.fullName, Vector3.zero, Quaternion.identity, false);
            //target.socket = this.FindSocket(name, target.entity.prefabID);

            BaseEntity newEnt = GameManager.server.CreateEntity(construction.fullName, Vector3.zero, Quaternion.identity, false);
            if (newEnt == null)
            {
                Server.Broadcast("deployable null");
                return;
            }

            Server.Broadcast($"Location: {newEnt.transform.position}");

            //Server.Broadcast($"Male Socket {construction.socketHandle.hierachyName}");

            //Server.Broadcast($"Looking for female socket: {construction.socketHandle.hierachyName}");
            Server.Broadcast($"Found male socket: {construction.socketHandle.hierachyName}");
            var socketName = construction.socketHandle.hierachyName.Substring(construction.socketHandle.hierachyName.IndexOf("/sockets/") + 9);
            socketName = socketName.Substring(0, socketName.IndexOf('/'));
            if (socketName.Contains("-male"))
                socketName = socketName.Replace("-male", "-female");
            else
                socketName += "-female";

            Server.Broadcast($"Looking for female socket: {socketName}");

            //newEnt.SetParent(parent);


            Socket_Base attachedSocket = null;
            var availableSockets = PrefabAttribute.server.FindAll<Socket_Base>(parent.prefabID);
            foreach (var socket in availableSockets)
            {
                if (!socket.male && socket.female && !socket.femaleDummy)
                {
                    Server.Broadcast($"Checking socket: {socket.hierachyName}");
                    if (socket.hierachyName.Contains("/" + socketName + "/"))
                    {
                        var rotation = parent.transform.rotation * socket.position;
                        var location = parent.transform.position;

                        Server.Broadcast($"Socket {socket.socketName} {socket.position} {socket.rotation}");
                        attachedSocket = socket;
                        break;
                    }
                }
            }


            if (construction.socketHandle)
            {
                Server.Broadcast($"Using socket handle {construction.deployable.slot} {construction.fullName} {construction.deployable.setSocketParent} {attachedSocket.worldPosition}");
                newEnt.SetParent(parent, socketName);

                newEnt.transform.position = newEnt.transform.position + attachedSocket.worldPosition;

                if (attachedSocket != null)
                    Effect.server.Run(deployable.placeEffect.resourcePath, newEnt.transform.TransformPoint(attachedSocket.worldPosition), newEnt.transform.up);
            }
            else
            {
                newEnt.SetParent(parent, true);
            }
             
            DecayEntity decayEntity = newEnt as DecayEntity;
            if (decayEntity != null)
            {
                decayEntity.AttachToBuilding(parent as DecayEntity);
            }
            newEnt.OwnerID = parent.OwnerID;
            newEnt.Spawn();

            Pool.Free(ref availableSockets);
        }

I also looked at the Build plugin, and it behaves the same way with hatches -- they just break when I try and use its Deploy function with hatches and windows

That looks like quite the mess. I made a Symmetry plugin that does all that and the code was very simple. Seems like your doing too much here. Look at how CopyPaste works for example. I don't bother with the build plugin myself.