private void CaptureAppearanceSettings(IClientAPI remoteClient, Primitive.TextureEntry textureEntry, byte[] visualParams, Vector3 avSize, WearableCacheItem[] cacheItems)
        {
            int maxCacheitemsLoop = cacheItems.Length;
            if (maxCacheitemsLoop > AvatarWearable.MAX_WEARABLES)
            {
                maxCacheitemsLoop = AvatarWearable.MAX_WEARABLES;
                m_log.WarnFormat("[CACHEDBAKES]: Too Many Cache items Provided {0}, the max is {1}.  Truncating!", cacheItems.Length, AvatarWearable.MAX_WEARABLES);
            }

            m_BakedTextureModule = m_scene.RequestModuleInterface<IBakedTextureModule>();
            if (cacheItems.Length > 0)
            {
//                m_log.Debug("[Cacheitems]: " + cacheItems.Length);
//                for (int iter = 0; iter < maxCacheitemsLoop; iter++)
//                {
//                    m_log.Debug("[Cacheitems] {" + iter + "/" + cacheItems[iter].TextureIndex + "}: c-" + cacheItems[iter].CacheId + ", t-" +
//                                      cacheItems[iter].TextureID);
//                }
               
                ScenePresence p = null;
                if (m_scene.TryGetScenePresence(remoteClient.AgentId, out p))
                {

                    WearableCacheItem[] existingitems = p.Appearance.WearableCacheItems;
                    if (existingitems == null)
                    {
                        if (m_BakedTextureModule != null)
                        {
                            WearableCacheItem[] savedcache = null;
                            try
                            {
                                if (p.Appearance.WearableCacheItemsDirty)
                                {
                                    savedcache = m_BakedTextureModule.Get(p.UUID);
                                    p.Appearance.WearableCacheItems = savedcache;
                                    p.Appearance.WearableCacheItemsDirty = false;
                                }

                            }
                            /*
                             * The following Catch types DO NOT WORK with m_BakedTextureModule.Get
                             * it jumps to the General Packet Exception Handler if you don't catch Exception!
                             * 
                            catch (System.Net.Sockets.SocketException)
                            {
                                cacheItems = null;
                            }
                            catch (WebException)
                            {
                                cacheItems = null;
                            }
                            catch (InvalidOperationException)
                            {
                                cacheItems = null;
                            } */
                            catch (Exception)
                            {
                               // The service logs a sufficient error message.
                            }
                            

                            if (savedcache != null)
                                existingitems = savedcache;
                        }
                    }
                    // Existing items null means it's a fully new appearance
                    if (existingitems == null)
                    {

                        for (int i = 0; i < maxCacheitemsLoop; i++)
                        {
                            if (textureEntry.FaceTextures.Length > cacheItems[i].TextureIndex)
                            {
                                Primitive.TextureEntryFace face = textureEntry.FaceTextures[cacheItems[i].TextureIndex];
                                if (face == null)
                                {
                                    textureEntry.CreateFace(cacheItems[i].TextureIndex);
                                    textureEntry.FaceTextures[cacheItems[i].TextureIndex].TextureID =
                                        AppearanceManager.DEFAULT_AVATAR_TEXTURE;
                                    continue;
                                }
                                cacheItems[i].TextureID =face.TextureID;
                                if (m_scene.AssetService != null)
                                    cacheItems[i].TextureAsset =
                                        m_scene.AssetService.GetCached(cacheItems[i].TextureID.ToString());
                            }
                            else
                            {
                                m_log.WarnFormat("[CACHEDBAKES]: Invalid Texture Index Provided, Texture doesn't exist or hasn't been uploaded yet {0}, the max is {1}.  Skipping!", cacheItems[i].TextureIndex, textureEntry.FaceTextures.Length);
                            }


                        }
                    }
                    else


                    {
                        // for each uploaded baked texture
                        for (int i = 0; i < maxCacheitemsLoop; i++)
                        {
                            if (textureEntry.FaceTextures.Length > cacheItems[i].TextureIndex)
                            {
                                Primitive.TextureEntryFace face = textureEntry.FaceTextures[cacheItems[i].TextureIndex];
                                if (face == null)
                                {
                                    textureEntry.CreateFace(cacheItems[i].TextureIndex);
                                    textureEntry.FaceTextures[cacheItems[i].TextureIndex].TextureID =
                                        AppearanceManager.DEFAULT_AVATAR_TEXTURE;
                                    continue;
                                }
                                cacheItems[i].TextureID =
                                    face.TextureID;
                            }
                            else
                            {
                                m_log.WarnFormat("[CACHEDBAKES]: Invalid Texture Index Provided, Texture doesn't exist or hasn't been uploaded yet {0}, the max is {1}.  Skipping!", cacheItems[i].TextureIndex, textureEntry.FaceTextures.Length);
                            }
                        }

                        for (int i = 0; i < maxCacheitemsLoop; i++)
                        {
                            if (cacheItems[i].TextureAsset == null)
                            {
                                cacheItems[i].TextureAsset =
                                    m_scene.AssetService.GetCached(cacheItems[i].TextureID.ToString());
                            }
                        }
                    }



                    p.Appearance.WearableCacheItems = cacheItems;
                    
                   

                    if (m_BakedTextureModule != null)
                    {
                        m_BakedTextureModule.Store(remoteClient.AgentId, cacheItems);
                        p.Appearance.WearableCacheItemsDirty = true;
                        
                    }
                }
            }
        }