@SuppressWarnings("unchecked")
	int makeConnections(float minDistance, float maxDistance, short order, int connectOperation, BitSet bsA, BitSet bsB) {
		String stateScript = "connect " + minDistance + " " + maxDistance + " " + StateManager.escape(bsA) + " "
				+ StateManager.escape(bsB) + " " + JmolConstants.getBondOrderNameFromOrder(order) + " "
				+ JmolConstants.connectOperationName(connectOperation) + ";";
		stateScripts.add(stateScript);
		Logger.debug("makeConnections(" + minDistance + "," + maxDistance + "," + order + "," + connectOperation + ","
				+ bsA + "," + bsB + ")");

		if (connectOperation == JmolConstants.DELETE_BONDS) {
			return deleteConnections(minDistance, maxDistance, order, bsA, bsB);
		}
		if (connectOperation == JmolConstants.AUTO_BOND) {
			return autoBond(order, bsA, bsB);
		}
		if (order == JmolConstants.BOND_ORDER_NULL)
			order = JmolConstants.BOND_COVALENT_SINGLE; // default
		float minDistanceSquared = minDistance * minDistance;
		float maxDistanceSquared = maxDistance * maxDistance;
		defaultCovalentMad = viewer.getMadBond();
		short mad = getDefaultMadFromOrder(order);
		int nNew = 0;
		int nModified = 0;
		for (int iA = atomCount; --iA >= 0;) {
			if (!bsA.get(iA))
				continue;
			Atom atomA = atoms[iA];
			Point3f pointA = atomA;
			for (int iB = atomCount; --iB >= 0;) {
				if (iB == iA)
					continue;
				if (!bsB.get(iB))
					continue;
				Atom atomB = atoms[iB];
				if (atomA.modelIndex != atomB.modelIndex)
					continue;
				if (atomA.alternateLocationID != atomB.alternateLocationID && atomA.alternateLocationID != 0
						&& atomB.alternateLocationID != 0)
					continue;
				Bond bondAB = atomA.getBond(atomB);
				if (JmolConstants.MODIFY_ONLY == connectOperation && bondAB == null)
					continue;
				if (JmolConstants.CREATE_ONLY == connectOperation && bondAB != null)
					continue;
				float distanceSquared = pointA.distanceSquared(atomB);
				if (distanceSquared < minDistanceSquared || distanceSquared > maxDistanceSquared)
					continue;
				if (bondAB != null) {
					bondAB.setOrder(order);
					nNew++;
				}
				else {
					bondAtoms(atomA, atomB, order, mad);
					nModified++;
				}
			}
		}
		Logger.info(nNew + " new bonds; " + nModified + " modified");
		return nNew + nModified;
	}